Python Error - ImportError: no module named 'w

2019-03-30 17:34发布

I am a beginner to Python and am having great difficulty in running my python googleppengine code locally on my machine.

My code looks as follows:

import json
import urllib
import os
import webapp2
from google.appengine.ext.webapp import template
import datetime
from google.appengine.ext import db


class Events(db.Model):
    venue_name = db.StringProperty()
    address = db.StringProperty()
    id = db.StringProperty()
    venue_id = db.StringProperty()

    # hire_date = db.DateProperty()
    # attended_hr_training = db.BooleanProperty()

class eventSearch(webapp2.RequestHandler):  
    def get(self):
        base_url = 'http://api.eventful.com/json/events/search?app_key=zGtDX6cwQjCRdkf6&l=dublin&?q=music'
        response = urllib.urlopen(base_url)
        html = response.read()
        html = json.loads(html)
        result = html['venues']
        result1 = result['venue']

When I run this code in my cmd prompt with command "python file.py", I receive the following error:

Traceback <most recent call last>:
File "file.py", line 4, in <module>
import webapp2
ImportError: No module named 'webapp2'

I have 1. Created a PythonPath as suggested in How to add to the pythonpath in Windows? within my system variables with directories: C:\Python33\DLLs;C:\Python33\Lib;C:\Python33\Lib\lib2to3;C:\Program Files (x86)\Google\google_appengine;C:\Program Files (x86)\Google\google_appengine\lib;

I have then also added both of the below directories into my "PATH" variable also, as recommended within answer - import webapp2 works on google-app-engine even though I don't have webapp2 installed
C:\Program Files (x86)\Google\google_appengine\;C:\Program Files (x86)\Google\google_appengine\lib

EDIT: After suggestions within the answers provided I too have realized that GAE does not support version 3.3 of Python I was trying to run it with in my previous part of my question. After uninstalling Python33 and installing Python27 instead, changing my system variables to reflect the new Python27, I am still having issues and my code will not upload with the GAE launcher. I receive the following errors within my log-console (GAE Launcher):

2013-04-14 22:59:19 Running command: "['C:\\Python27\\pythonw.exe', 'C:\\Program Files (x86)\\Google\\google_appengine\\dev_appserver.py', '--skip_sdk_update_check=yes', '--port=8080', '--admin_port=8001', 'C:\\Users\\Karen\\Desktop\\Development\\projects\\file']"
Traceback (most recent call last):
  File "C:\Program Files (x86)\Google\google_appengine\dev_appserver.py", line 193, in     <module>
    _run_file(__file__, globals())
  File "C:\Program Files (x86)\Google\google_appengine\dev_appserver.py", line 189, in _run_file
    execfile(script_path, globals_)
  File "C:\Program Files     (x86)\Google\google_appengine\google\appengine\tools\devappserver2\devappserver2.py", line 30, in <module>
    from google.appengine.datastore import datastore_stub_util
  File "C:\Program Files     (x86)\Google\google_appengine\google\appengine\datastore\datastore_stub_util.py", line 45,     in <module>
    from google.appengine.api import api_base_pb
  File "C:\Program Files     (x86)\Google\google_appengine\google\appengine\api\api_base_pb.py", line 20, in <module>
    from google.net.proto import ProtocolBuffer
  File "C:\Program Files (x86)\Google\google_appengine\google\net\proto\ProtocolBuffer.py", line 22, in <module>
    import httplib
  File "C:\Python27\lib\httplib.py", line 71, in <module>
    import socket
  File "C:\Python27\lib\socket.py", line 47, in <module>
    import _socket
ImportError: Module use of python25.dll conflicts with this version of Python.
2013-04-14 22:59:21 (Process exited with code 1)

Thank you for any help that you can provide me.

4条回答
趁早两清
2楼-- · 2019-03-30 18:14

You should not install webapp2. It is included in the SDK, and is already in the production runtime.

Have a read of Configuring libraries that are part of the appengine environment https://developers.google.com/appengine/docs/python/python25/migrate27#Configuring_Libraries

and here is the list of included 3rd party libs.

https://developers.google.com/appengine/docs/python/tools/libraries27

If you use pip/easy_install for various other libs, you will find on its own that is insufficient. You will need to link or include these libs in your project, manipulate sys.path so they can be found, and make sure these libraries are deployed.

查看更多
Animai°情兽
3楼-- · 2019-03-30 18:16

This solved my problem (same problem you got)

First don't try to run from python IDLE run from Google app engine open localhost:port

Open log in GAE if still showing same error try below steps

  1. Check your python version 2.7.X or 3.X
  2. If 3.x install 2.7.8
  3. Then open google app engine console and go to edit > preferences
  4. add your python27 directory (ex:C:\Python27\pythonw.exe) to PythonPath click ok
查看更多
forever°为你锁心
4楼-- · 2019-03-30 18:23

You can install webapp2 by using pip or easy_install. Refer to http://webapp-improved.appspot.com/tutorials/quickstart.nogae.html for quick stark

查看更多
走好不送
5楼-- · 2019-03-30 18:34

It looks like appengine/tools/devappserver2/python/sandbox.py is supposed to turn C:\path\to\google_appengine\google into C:\path\to\google_appengine, but there is an extra dirname, so it ends up getting C:\path\to. I'm not sure why it only causes problems in some circumstances.

You can work around the problem by changing:

library_pattern = os.path.join(os.path.dirname(
    os.path.dirname(google.__file__)), _THIRD_PARTY_LIBRARY_FORMAT_STRING)

to:

library_pattern = os.path.join(
    os.path.dirname(google.__file__), _THIRD_PARTY_LIBRARY_FORMAT_STRING)

I found this issue by putting raise Exception(sys.path) in various places in the app engine code and restarting the dev server.

查看更多
登录 后发表回答