ImportError; and zipimporters disappeared from pkgutil.iter_importers() - python

I have a Django instance that's mysteriously incapable of import memcache ... after (some unknown event that happens after a period of running just fine.)
LAMP configuration:
RHEL 5.7
Apache 2.2.3
mod_wsgi 2.3 (dynamically linked to Python2.5.4 .so)
Python 2.5.4
Django 1.2
memcache sits in: /usr/local/lib/python2.5/site-packages/python_memcached-1.44-py2.5.ee/memcache.pyc
If I open up a command shell, and import memcache, it imports just fine.
And for a time, in Django, import memcache works just fine.
But after some unknown event, the import fails: ImportError: No module named memcache
Just prior point of failure, I logged the system path, and the path explicitly includes /usr/local/lib/python2.5 and /usr/local/lib/python2.5/site-packages.
I also logged the response to pkgutil.iter_importers(), and found something interesting: At the point of failure, iter_importers features NONE of the zipimporters -- and it is a zipimporter that is required to look inside the egg and find memcache.
If I manually import memcache, it functions:
try:
import memcache
except ImportError:
import zipimport
zi = zipimport.zipimporter('/usr/local/lib/python2.5/site-packages/python_memcached-1.44-py2.5.egg')
memcache = zi.load_module('memcache')
What is going on? What can I do to make it work without the workaround?

Okay. I found the answer. There is a bug in Python 2.5's os.listdir C implementation on 64-bit computers. When I applied the patch, everything works permanently.

Related

requests segfaults when embedded as wsgi, but not standalone

import sys
import os
import logging
# need to add environment to apache's path for includes
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "../../")))
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "../")))
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), ".")))
# likewise add cherrypy/other modules
sys.path.append(os.path.abspath("/Library/Python/2.7/site-packages/"))
import requests
response = requests.get('http://www.google.com').text
Using Python 2.7.6, requests 2.7.0, and Apache under MAMP 3.0, the above code crashes. A quick look through the code using winpdb seems to suggest that actually trying to open an internet connection is what is crashing the Python process. The Apache log is not very helpful, only saying
[notice] child pid 18879 exit signal Segmentation fault (11)
While my full code uses Cherrypy 3.8 to provide the WSGI portion of the framework, I feel that it is irrelevant to the problem at hand.
Is this some known problem with requests+apache, or is it some other problem? Python crashing without any comments makes it hard for me to even think of a way to start solving this issue.
EDIT: Using pdb, I found that the program segfaults on line 1421 of urllib.py in the python standard library.
proxy_settings = _get_proxy_settings()
where _get_proxy_settings comes from _scproxy.
I still have no idea how to fix this.
It seems that this is a platform-specific bug for my version of MacOS.

Error importing simplejson and antlr3 to djangoappengine project

I'm building a project that uses django-app-engine, but get some import errors when running the project with the GAE launcher.
My first error is importing simplejson, same as: Error importing simplejson after upgrading to appengine dev server 1.7.6
I've resolved that issue by modifying old_dev_appserver.py in the GAE bundle to import 'json' instead of 'simplejson' (I hope it's ok that I modified the GAE bundle..)
After the change, I receive a new error:
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/cron/groc.py", line 31, in <module>
import antlr3
ImportError: No module named antlr3
In the link above, #trope actually commented on having the same issue, but I thought a new question would be better than adding comments to the original issue (which is resolved by changing simplejson to json)
As suggested here: Django App Engine can't find antlr3 module, I've tried to add
os.path.join(SDK_PATH, 'lib', 'antlr3')
And also tried to install the antlr3 runtime.
I feel this issue is different to the one above because it's a direct result of the simplejson workaround, but perhaps it is the same.
Python 2.7.2, GAELauncher 1.9.4
Any suggestions?
edit: I've also now tried the exact same setup/codebase on my PC with the latest version of Launcher, and I get the exact same two errors: first simplejson, then antlr3. Maybe that means it's an issue with the codebase/djangoappengine install?
OK, this should be solved. Make sure you have updated all the django nonrel projects (dbindexer, django, djangoappengine, djangotoolbox, and whatever else you use). Also, run gcloud components update from the command line.
In addition to adding os.path.join(SDK_PATH, 'lib', 'antlr3'), did you also actually download and install it? From the command line, run whereis antlr3.

The difference between 'from pylons import config' and 'import pylons.config'

Im trying to import a company module into my software and I get the error:
ImportError: No module named config
from:
from pylons.config import config
So obviously, the module that im importing requires pylons.config but cant find it in my virtual environment.
If I go to the terminal and try some Python scripts I can seem to find the config file if I try:
from pylons import config
but will error if I try:
import pylons.config
Why is this?
And does anybody how or where I can get:
from pylons.config import config
to work. Bearing in mind that I cannot change the code for this module, only mine which is importing it or my own system files.
UPDATE
If anyone finding this page has a similar problem you may find that you are trying to run two modules with different versions of Pylons.
For example, you are creating a login application called myApp. You have some Python modules which help with login handling called pyLogin.
First you install pyLogin with python setup.py install. This adds the libraries to your site packages and updates any libraries it depends on, such as SqlAlchemy.
Next you install myApp in the same way which again updates libraries and dependencies.
This problem will occur if pyLogin and myApp are using different versions of Pylons. If pyLogin is using Pylons 0.9.6 and myApp is using Pylons 1.0 for example, then the pyLogin code will be called from myApp but it will be running in the wrong Pylons framework and hence will require EITHER from pylons import config or from pylons.config import config, but will only work with one. If it is using the wrong call for Pylons then you will find yourself with this error message.
So the only solution to this error is to either find earlier or later libraries which use the same Pylons version as your application or to convert your application to the same Pylons version as the libraries you are using.
There is a diffrence between two usages...
import loads a Python module into its own namespace, while from loads a Python module into the current namespace.
So, using from pylons import config imports config to to your current namespace. But trying to import a class or function using import is not possible since there is no namespace to keep them... You can only import modules, and use functions or classes via calling them with their own namespace like
import pylons
....
pylons.config #to retreive config
More about import in Python

How to write a unittest for importing a module in Python

What is the pythonic way of writing a unittest to see if a module is properly installed? By properly installed I mean, it does not raise an ImportError: No module named foo.
As I have to deploy my Django
application on a different server and
it requires some extra modules I want
to make sure that all required modules
are installed.
This is not a unit test scenario at all.
This is a production readiness process and it isn't -- technically -- a test of your application.
It's a query about the environment. Ours includes dozens of things.
Start with a simple script like this. Add each thing you need to be sure exists.
try:
import simplejson
except ImportError:
print "***FAILURE: simplejson missing***"
sys.exit( 2 )
sys.exit( 0 )
Just run this script in each environment as part of installation. It's not a unit test at all. It's a precondition for setup install.
I don't see why you'd need to test this, but something like:
def my_import_test(self):
import my_module
If an import error is raised the test has failed, if not it passes.

Python and MySQL

I can get Python to work with Postgresql but I cannot get it to work with MySQL. The main problem is that on the shared hosting account I have I do not have the ability to install things such as Django or PySQL, I generally fail when installing them on my computer so maybe it's good I can't install on the host.
I found bpgsql really good because it does not require an install, it's a single file that I can look at, read and then call the functions of. Does anybody know of something like this for MySQL?
MySQLdb is what I have used before.
If you host is using Python version 2.5 or higher, support for sqlite3 databases is built in (sqlite allows you to have a relational database that is simply a file in your filesystem). But buyer beware, sqlite is not suited for production, so it may depend what you are trying to do with it.
Another option may be to call your host and complain, or change hosts. Honestly these days, any self respecting web host that supports python and mysql ought to have MySQLdb pre installed.
I don't have any experience with http://www.SiteGround.com as a web host personally.
This is just a guess, but it's common for a shared host to support Python and MySQL with the MySQLdb module (e.g., GoDaddy does this). Try the following CGI script to see if MySQLdb is installed.
#!/usr/bin/python
module_name = 'MySQLdb'
head = '''Content-Type: text/html
%s is ''' % module_name
try:
__import__(module_name)
print head + 'installed'
except ImportError:
print head + 'not installed'
I uploaded it and got an internal error
Premature end of script headers
After much playing around, I found that if I had
import cgi
import cgitb; cgitb.enable()
import MySQLdb
It would give me a much more useful answer and say that it was not installed, you can see it yourself -> http://woarl.com/db.py
Oddly enough, this would produce an error
import MySQLdb
import cgi
import cgitb; cgitb.enable()
I looked at some of the other files I had up there and it seems that library was one of the ones I had already tried.
You could try setting up your own python installation using Virtual Python. Check out how to setup Django using it here. That was written a long time ago, but it shows how I got MySQLdb setup without having root access or anything like it. Once you've got the basics going, you can install any python library you want.
You really want MySQLdb for any MySQL + Python code. However, you shouldn't need root access or anything to use it. You can build/install it in a user directory (~/lib/python2.x/site-packages), and just add that to your PYTHON_PATH env variable. This should work for just about any python library.
Give it a shot, there really isn't a good alternative.
Take a pick at
https://docs.djangoproject.com/en/1.8/ref/databases/
MySQLdb is mostly used driver, but if you are using python3 and django 1.8.x that will not work, then you should use mysqlclient that is a folk of MySQLdb on the following link
https://pypi.python.org/pypi/mysqlclient

Categories

Resources