0

I am trying to run the following script in my virtualenv:

(python2.7.2-gonvaled1)gonvaled@lycaon:~/.virtualenvs/python2.7.2-gonvaled1$ couchy.py
Traceback (most recent call last):
 File "/home/gonvaled/projects/bin/couchy.py", line 46, in <module>
 from couchdb_support import CouchdbLists
 File "/home/gonvaled/projects/test_project/python_modules/couchdb_support.py", line 3615, in <module>
 from asterisk_support import AsteriskSupport
 File "/home/gonvaled/projects/test_project/python_modules/asterisk_support.py", line 4, in <module>
 from starpy_support import AmiCommand
 File "/home/gonvaled/projects/test_project/python_modules/starpy_support.py", line 7, in <module>
 from starpy import manager, fastagi, utilapplication, menu
 File "/usr/lib/python2.5/site-packages/starpy/utilapplication.py", line 2, in <module>
 from basicproperty import common, propertied, basic, weak
 File "/usr/lib/python2.5/site-packages/basicproperty-0.6.12a-py2.5-linux-i686.egg/basicproperty/common.py", line 159, in <module>
 from basictypes import datedatetime_types as ddt
 File "/usr/lib/python2.5/site-packages/basicproperty-0.6.12a-py2.5-linux-i686.egg/basictypes/datedatetime_types.py", line 4, in <module>
 from dateutil import parser 
 File "/usr/lib/python2.5/site-packages/python_dateutil-2.1-py2.5.egg/dateutil/parser.py", line 8
 from __future__ import unicode_literals
SyntaxError: future feature unicode_literals is not defined

So somehow python is trying to import from:

/usr/lib/python2.5/site-packages/starpy/utilapplication.py

This does not make any sense. My virtualenv is very clean:

(python2.7.2-gonvaled1)gonvaled@lycaon:~/.virtualenvs/python2.7.2-gonvaled1$ python
Python 2.7.2 (default, Mar 29 2012, 00:31:29) 
[GCC 4.2.4 (Ubuntu 4.2.4-1ubuntu4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path 
['', '/home/gonvaled/.virtualenvs/python2.7.2-gonvaled1/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg', '/home/gonvaled/.virtualenvs/python2.7.2-gonvaled1/lib/python2.7/site-packages/pip-1.1-py2.7.egg', '/home/gonvaled/.virtualenvs/python2.7.2-gonvaled1/lib/python2.7/site-packages/Twisted-12.0.0-py2.7-linux-i686.egg', '/home/gonvaled/.virtualenvs/python2.7.2-gonvaled1/lib/python2.7/site-packages/zope.interface-3.8.0-py2.7-linux-i686.egg', '/home/gonvaled/.virtualenvs/python2.7.2-gonvaled1/lib/python2.7/site-packages/basicproperty-0.6.12a-py2.7-linux-i686.egg', '/home/gonvaled/projects/gonvaled_settings', '/home/gonvaled/django_apps', '/home/gonvaled/projects/test_project/ipc', '/home/gonvaled/projects/test_project/python_modules', '/home/gonvaled/projects/test_project/gdata', '/home/gonvaled/projects/callisto/libraries', '/home/gonvaled/.virtualenvs/python2.7.2-gonvaled1', '/home/gonvaled/.virtualenvs/python2.7.2-gonvaled1/lib/python27.zip', '/home/gonvaled/.virtualenvs/python2.7.2-gonvaled1/lib/python2.7', '/home/gonvaled/.virtualenvs/python2.7.2-gonvaled1/lib/python2.7/plat-linux2', '/home/gonvaled/.virtualenvs/python2.7.2-gonvaled1/lib/python2.7/lib-tk', '/home/gonvaled/.virtualenvs/python2.7.2-gonvaled1/lib/python2.7/lib-old', '/home/gonvaled/.virtualenvs/python2.7.2-gonvaled1/lib/python2.7/lib-dynload', '/usr/local/python/2.7.2/lib/python2.7', '/usr/local/python/2.7.2/lib/python2.7/plat-linux2', '/usr/local/python/2.7.2/lib/python2.7/lib-tk', '/home/gonvaled/.virtualenvs/python2.7.2-gonvaled1/lib/python2.7/site-packages']

And:

(python2.7.2-gonvaled1)gonvaled@lycaon:~/.virtualenvs/python2.7.2-gonvaled1$ pip freeze
CouchDB==0.9dev
DateUtils==0.5.1
Twisted==12.0.0
basicproperty==0.6.12a
pystache==0.4.0
python-dateutil==2.1
pytz==2012b
simplejson==2.4.0
six==1.1.0
starpy==1.0.0a12
wsgiref==0.1.2
zope.interface==3.8.0

So to summarize: python is importing from /usr/lib/python2.5/site-packages even though I am inside a virtualenv. How can this be?

asked Mar 28, 2012 at 23:29

2 Answers 2

2

Try running the script as:

$ python couch.py

in your activated virtualenv. Depending on your system and the first line (the shebang line) of the file, you may not be running in the Python you think you are.

answered Mar 29, 2012 at 0:20
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, that was it. My shebang is #!/usr/bin/python. I find a bit annoying that I can not run my python scripts by themselves. It means that, to run them in a virtualenv, I must do something different than what I do in the host environment. It effectively kills the concept of shebang, since to make this general, I can not rely on it (think control scripts, cronjobs, etc). I have tried to put #!python in the shebang, but apparently absolute paths are required.
Use a shebang line of #!/usr/bin/env python . That should use the first python found on the current $PATH which should be the one in your active virtualenv.
0

You should create your virtualenv with the --nositepackages option to keep it clean and away from your system Python.

answered Mar 28, 2012 at 23:39

1 Comment

No, no, I already said that my virtualenv was very clean. It was actually created using --nositepackages (which, by the way, is no longer required in recent versions of virtualenv, since it has become the default)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.