简体   繁体   中英

Unable to import _winreg in Python 2.7.9 virtual environment

I'm running an app engine application in a virtual environment on windows 7 64bit, python 2.7.9 x64.

Here's the stacktrace:

    p_system = platform.system()
  File "C:\Python27\lib\platform.py", line 1310, in system
    return uname()[0]
  File "C:\Python27\lib\platform.py", line 1206, in uname
    release,version,csd,ptype = win32_ver()
  File "C:\Python27\lib\platform.py", line 597, in win32_ver
    import _winreg
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\devappserver2\python\sandbox.py", line 945, in load_module
    raise ImportError('No module named %s' % fullname)
  ImportError: No module named _winreg

However, it works just fine from cli (outside venv):

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Users\Admin>python
Python 2.7.9 (default, Dec 10 2014, 12:28:03) [MSC v.1500 64 bit (AMD64)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import _winreg
>>> import platform
>>> platform.system()
'Windows'
>>>

Why does this happen? What can I do to fix this?

Module _winreg , as the docs say, exists to "expose the Windows registry API to Python".

App Engine does not supply a "Windows registry API" (nor any other Windows-specific API). Therefore, its sandbox blocks attempts to import the module -- note, at the end of your stack trace, that the exception is deliberately raised in module sandbox.py of the App Engine SDK.

Python's "virtual env" plays no part here -- it's all about App Engine.

Please clarify what task you're trying to accomplish with _winreg once your GAE app is deployed -- assume it's deployed to Linux servers (although the GAE runtime doesn't supply Linux-specific APIs either:-), so there is no Windows Registry API anywhere in the neighborhood...

The workaround provided by Google, until a fix is implemented, is as follows:

  • Go to: <sdk_root>\\google\\appengine\\tools\\devappserver2\\python\\sandbox.py
  • Find the definition of _WHITE_LIST_C_MODULES = [xxx]
  • Add the following two lines to the list:

'_winreg',

'_ctypes',

If this does not succeed, run python -m pip install google-cloud

I think that the problem is that GAE is not aware that you are in development mode, I suppose because the SERVER_SOFTWARE variable is set to something not starting with "Dev".

If you execute the following code (before calling any GAE library) it should fix the issue:

import os
os.environ['SERVER_SOFTWARE'] = 'Dev'

Note: Make sure this code is removed before going to production.

I had this problem a few days ago.

As said above, the GAE sandbox on Windows blocks some routines or libraries, even built-in one, because it is developed to Unix-like platform.

I opened an issue to Google Team and they passed a workaround:

https://issuetracker.google.com/issues/38290292

That workaround worked well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM