Unable to import _winreg in Python 2.7.9 virtual environment - python

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.

Related

Python can't connect to Oracle Database, close_fds is not supported

I have this small script which tryes to connect to a server running a oracle database (11g).
import os
import sys
import jpype
import jaydebeapi
if("JAVA_HOME" not in os.environ):
os.environ["JAVA_HOME"] = "c:\Program Files\Java\jdk1.8.0_45"
ODBC_DRIVER = os.path.join(os.path.dirname(os.path.abspath(__file__)), "ojdbc6.jar")
print("\tPYTHON VERSION", sys.version)
print("\tJAVA_HOME", os.environ["JAVA_HOME"])
print("\tDEFAULT JVM PATH", jpype.getDefaultJVMPath())
print("\tODBC_DRIVER", ODBC_DRIVER)
try:
jpype.startJVM(jpype.getDefaultJVMPath(), "-Djava.class.path={}".format(ODBC_DRIVER))
conn = jaydebeapi.connect("oracle.jdbc.driver.OracleDriver",
["jdbc:oracle:thin//192.168.10.33:1521", "<user>", "<passw>"],
ODBC_DRIVER)
except Exception as e:
print(e)
sys.exit(-1)
sys.exit(0)
The output with the thrown exception:
PYTHON VERSION 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:44:40) [MSC v.1600 64 bit (AMD64)]
JAVA_HOME c:\Program Files\Java\jdk1.8.0_45
DEFAULT JVM PATH c:\Program Files\Java\jdk1.8.0_45\jre\bin\server\jvm.dll
ODBC_DRIVER d:\path\to\ojdbc6.jar
close_fds is not supported on Windows platforms if you redirect stdin/stdout/stderr
As a reference I mostly used small tutorials which connect to a oracle database. Their code lookst essentially the same.
By looking for a solution I have found that python should support close_fds from version 2.6.x
I'm not sure where to start looking.
More information on jpype and jaydebaapi:
JayDeBeApi3 (1.3)
JPype1-py3 (0.5.5.2)
Both were installed through pip.
I would recommend using python >=2.7.x (which is fine in your case because you are using a higher version).
Use JPype1 0.5.7 as recommended by JayDeBeApi 0.2.0, which is the latest version.
Also, since you are starting the JVM yourself, you don't need to specify the 3rd argument (driver) in the connect statement. Or you can comment out the startjvm and keep connect statement as it is. Jpype does the same (i.e, startjvm if you specify the driver jar as 3rd argument in connect statement).

Python 2 Bindings Net-SNMP Error - undefined symbol: netsnmp-memdup

I successfully compiled net-snmp-5.7.3 on Ubuntu. :D This is the specific version of Ubuntu:
Linux loserBox 3.13.0-32-generic #57-Ubuntu SMP Tue Jul 15 03:51:08 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
I seemed to have successfully installed the Python bindings for net-snmp too. This is included with the net-nsmp download as a different folder titled 'Python' with the setup.py file in it. However when running the command python setup.py test I noticed some problems. I thought this may be nothing to worry about so I went ahead and executed python setup.py install I was not sure what the instructions meant in the README file that say, "python setup.py test (requires a locally running agent w/ config provided)". So, this is why I installed it with the failed test or something. Anyways, I am not sure if this is a problem or not.
After successfully installing the python bindings for net-snmp I switched directories back to my Desktop and opened up an interactive python shell. From here I imported the netsnmp module and received the below error. It almost looks like there is a spelling error in the variable netsnmp_memdup that is throwing the error in the Traceback. This looks like a problem with the C code and not python.
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import netsnmp
/usr/lib/python2.7/dist-packages/pkg_resources.py:1031: UserWarning:
/home/loser_user/.python-eggs is writable by group/others and vulnerable to attack
when used with get_resource_filename. Consider a more secure location
(set with .set_extraction_path or the PYTHON_EGG_CACHE environment variable).
warnings.warn(msg, UserWarning)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "build/bdist.linux-x86_64/egg/netsnmp/__init__.py", line 1, in <module>
File "build/bdist.linux-x86_64/egg/netsnmp/client.py", line 1, in <module>
File "build/bdist.linux-x86_64/egg/netsnmp/client_intf.py", line 7, in <module>
File "build/bdist.linux-x86_64/egg/netsnmp/client_intf.py", line 6, in __bootstrap__
ImportError: /home/loser_user/.python-eggs/netsnmp_python-1.0a1-py2.7-linux-x86_64.egg-
tmp/netsnmp/client_intf.so: undefined symbol: netsnmp_memdup
Does anyone know how to fix this problem? I looked on the mailing list page on sourceforge for this project and searched the supoort archives but did not find anything.
Thanks for listening to a crazy man's Python problems.
Happy Holidays,
user_loser
Alrgihty, thanks to my good friend Naveen, we have traced this down to an actual bug in the Python Net-SNMP bindings in the 5.7.3.
There are two ways around this:
Use the Python bindings in Net-SNMP 5.7.2
See this commit on our fork of the Net-SNMP Python library (only started yesterday so bear with us): https://github.com/fgimian/easysnmp/commit/fa86af977b563f65e7d70243752d48b94a8d5686 and replicate this in your download of Net-SNMP.

import wx failed under GDB python interface, R6034: An application has made an attempt to load the C runtime library incorrectly

I have a self build GDB.exe under WinXP, which is link against python 2.7 dll. The GDB was built under msys+MinGW, so GDB.exe is linked against msvcrt.dll. But the python dll is linked against msvcrt90.dll. See the screen shot below of the dependency walker.
Generally, the python interpreter in GDB.exe works quite well, and I can correctly run python pretty printer and other python commands.
But I found that the wxPython can not run correctly, see the command log:
GNU gdb (GDB) 7.8.50.20140717-cvs
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "mingw32".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word".
(gdb) python import wx
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "E:\code\gdb\mybuild\bin\lib\site-packages\wx-3.0-msw\wx\__init__.py", li
ne 45, in <module>
from wx._core import *
File "E:\code\gdb\mybuild\bin\lib\site-packages\wx-3.0-msw\wx\_core.py", line
4, in <module>
import _core_
ImportError: DLL load failed: The specified module could not be found.
Error while executing Python code.
(gdb)
I realize that there are some dlls under folder: E:\code\gdb\mybuild\bin\Lib\site-packages\wx-3.0-msw\wx, such as "core_.pyd" and "wxmsw30u_core_vc90.dll", these dlls were build from Visual C++, and you can see, they are linked to msvcr90.dll and msvcp90.dll.
But if I run the same command under python.exe (which is E:\code\gdb\mybuild\bin\python.exe) command line shell, there is not such issue, see the log:
Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import wx
>>>
Look, I don't have any issue to run import wx command.
I also noticed that the python.exe is build against msvc90.dll, see the dependency walker screen shot below:
Now, my question is: how to solve this issue? From what I know, the two different kind of DLL (msvcrt.dll and msvcrt90.dll) works peacefully, but why I can't use wxpython? Thank you.
OK, the problem is solved. The reason is that both the pyd file and the dll file under the folder E:\code\gdb\mybuild\bin\Lib\site-packages\wx-3.0-msw\wx need to be associated with a manifest. I manually add the manifest to the pyd file by a tool named Resource Hacker, but I think any tool which can edit the PE file can do this. The pyd file already have some piece of manifest, so I just add more like:
<dependency>
<dependentAssembly>
<assemblyIdentity type='win32' name='Microsoft.VC90.CRT' version='9.0.21022.8' processorArchitecture='x86' publicKeyToken='1fc8b3b9a1e18e3b' />
</dependentAssembly>
</dependency>
For the dll file, I just found there are manifest file in a wxpython development package 32-bit binaries for MSVC 9 from official wxPython site, so I just copy the manifest files to the E:\code\gdb\mybuild\bin\Lib\site-packages\wx-3.0-msw\wx.
I don't have issue now, I can correctly show a wxFrame from GDB's command line.
I have tried the process of ollydbg23. The changes taken effect only after restart.

Unable to get pyrabbit to run

Im working on a little project that running rabbitmq with python, I need a way to access the management api and pull stats, jobs, etc. I have tried using pyRabbit, but doen't appear to be working unsure why, hoping better programmers might know? Below I was just following the basic tutorial and readme to perform the very basic task. My server is up, I'm able to connect outside of python and pyrabbit fine. I have installed off the dependencies with no luck, at least I think. Also open to other suggestions for just getting queue size, queues, active clients etc outside of pyRabbit.
'Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:\Users\user>python
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
import nose
import httplib2
import mock
from pyrabbit.api import Client
import pyrabbit
cl = Client('my.ip.com:15672', 'guest', 'guest')
cl.is_alive()
No JSON object could be decoded - (Not found.) ()
Traceback (most recent call last):
File "", line 1, in
File "C:\Python27\lib\site-packages\pyrabbit\api.py", line 48, in wrapper if self.has_admin_rights:
File "C:\Python27\lib\site-packages\pyrabbit\api.py", line 175, in has_admin_right whoami = self.get_whoami()
File "C:\Python27\lib\site-packages\pyrabbit\api.py", line 161, in get_whoami whoami = self.http.do_call(path, 'GET')
File "C:\Python27\lib\site-packages\pyrabbit\http.py", line 112, in do_call raise HTTPError(content, resp.status, resp.reason, path, body)
pyrabbit.http.HTTPError: 404 - Object Not Found (None) (whoami) (None)'
I was never able to solve this. But, this forced me to learn what json is, I used simplejson along with httplib2 and it worked like a charm...
This might be a rabbitmq configuration problem: as it is written here: https://www.rabbitmq.com/access-control.html, "guest" user can only connect via localhost:
By default, the guest user is prohibited from connecting to the broker remotely; it can only connect over a loopback interface (i.e. localhost). This applies both to AMQP and to any other protocols enabled via plugins. Any other users you create will not (by default) be restricted in this way.
This is configured via the loopback_users item in the configuration file.
If you wish to allow the guest user to connect from a remote host, you should set the loopback_users configuration item to []. A complete >rabbitmq.config which does this would look like:
[{rabbit, [{loopback_users, []}]}].

Local Appengine stopped working

I tried to run one of my AppEnigne projects (python) today but it will no longer launch, this is the stack trace I'm getting.
*** Running dev_appserver with the following flags:
--admin_console_server= --port=8080 --clear_datastore
Python command: /usr/bin/python2.5
Traceback (most recent call last):
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/dev_appserver.py", line 77, in <module>
run_file(__file__, globals())
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/dev_appserver.py", line 73, in run_file
execfile(script_path, globals_)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver_main.py", line 138, in <module>
import logging
ImportError: No module named logging
I thought it could be a python 2.6 error but I adjusted my path to /usr/bin/python2.5 and its still not working. I'm running OSX 10.6.8 and have the latest AppEngineLauncher 1.5.4
The only thing I changed recently that might have affected this is when I updated my XCode to the latest version, v4.2 build 4C199
Has anyone else faced this issue recently?
EDIT
I can't import logging from the terminal either, same message. Here's Python's path.
Chriss-MacBook-Pro:bin chris$ /usr/bin/python2.5
Python 2.5.4 (r254:67916, Aug 2 2010, 20:09:39)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python25.zip',
'/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5',
'/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/plat-darwin',
'/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/plat-mac',
'/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/plat-mac/lib-scriptpackages',
'/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python',
'/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/lib-tk',
'/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/lib-dynload',
'/Library/Python/2.5/site-packages',
'/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python/PyObjC',
'/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python/wx-2.8-mac-unicode']
>>>
Thanks to Nick and Wobble I've figured it out. I recently updated my XCode install to the 4.2 GM release and removed the beta versions. Along the way OSX forgot where gcc was installed and prevented it from compiling the python modules like logging. This resulted in missing .pyo files inside /System/Library/Frameworks/Python.framework/Versions/2.5/lib/python25/logging which prevents import logging from working.
Logging wasn't the only module that wasn't compiled, just the first one AppEngine tried to import.
Solution: uninstall and do a clean install of XCode. Make sure gcc can be found on your PATH and everything should be fine.

Categories

Resources