简体   繁体   English

无法在Python 2.7.9虚拟环境中导入_winreg

[英]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. 我在Windows 7 64位,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): 但是,从cli(在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". 正如文档所说,模块_winreg存在是为了“将Windows注册表API暴露给Python”。

App Engine does not supply a "Windows registry API" (nor any other Windows-specific API). App Engine不提供“Windows注册表API”(也不提供任何其他Windows特定的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. 因此,它的沙箱块会尝试import模块 - 请注意,在堆栈跟踪结束时,App Engine SDK的模块sandbox.py中会故意引发该异常。

Python's "virtual env" plays no part here -- it's all about App Engine. Python的“虚拟环境”在这里不起作用 - 它都是关于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... 请说明一旦部署了GAE应用程序,您要尝试使用_winreg完成_winreg任务 - 假设它已部署到Linux服务器(尽管GAE运行时不提供特定于Linux的API :-),因此没有Windows注册表API附近的任何地方......

The workaround provided by Google, until a fix is implemented, is as follows: 在实施修复程序之前,Google提供的解决方法如下:

  • Go to: <sdk_root>\\google\\appengine\\tools\\devappserver2\\python\\sandbox.py 转到: <sdk_root>\\google\\appengine\\tools\\devappserver2\\python\\sandbox.py
  • Find the definition of _WHITE_LIST_C_MODULES = [xxx] 查找_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 如果这不成功,请运行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". 我认为问题是GAE不知道你处于开发模式,我想因为SERVER_SOFTWARE变量被设置为不以“Dev”开头的东西。

If you execute the following code (before calling any GAE library) it should fix the issue: 如果您执行以下代码(在调用任何GAE库之前),它应该解决问题:

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. 如上所述,Windows上的GAE沙箱阻止了一些例程或库,甚至是内置的例程,因为它是开发到类Unix平台的。

I opened an issue to Google Team and they passed a workaround: 我向Google小组开了一个问题,他们通过了一个解决方法:

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

That workaround worked well. 这种解决方法效果很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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