I am using virtualenv and virtualenvwrapper on a Mac OS X machine. My project layout is:
/Users/mrafayaleem/Projects/imagemonster/ (project dir)
|---- imagemonster/
|---- server.py
|---- conf/
|---- settings.py
I am getting Import error on the following line in server.py:
from imagemonster.conf import settings
when I run it using python imagemonster/server.py
from the project directory. Running it under ipyhton
works just fine and I can't understand why this is happening.
Following are sys.paths for both:
Python:
/Users/mrafayaleem/.virtualenvs/imagemonster/lib/python27.zip
/Users/mrafayaleem/.virtualenvs/imagemonster/lib/python2.7
/Users/mrafayaleem/.virtualenvs/imagemonster/lib/python2.7/plat-darwin
/Users/mrafayaleem/.virtualenvs/imagemonster/lib/python2.7/plat-mac
/Users/mrafayaleem/.virtualenvs/imagemonster/lib/python2.7/plat-mac/lib-scriptpackages
/Users/mrafayaleem/.virtualenvs/imagemonster/Extras/lib/python
/Users/mrafayaleem/.virtualenvs/imagemonster/lib/python2.7/lib-tk
/Users/mrafayaleem/.virtualenvs/imagemonster/lib/python2.7/lib-old
/Users/mrafayaleem/.virtualenvs/imagemonster/lib/python2.7/lib-dynload
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages
/Users/mrafayaleem/.virtualenvs/imagemonster/lib/python2.7/site-packages
iPython:
/Users/mrafayaleem/.virtualenvs/imagemonster/bin
/Users/mrafayaleem/.virtualenvs/imagemonster/lib/python27.zip
/Users/mrafayaleem/.virtualenvs/imagemonster/lib/python2.7
/Users/mrafayaleem/.virtualenvs/imagemonster/lib/python2.7/plat-darwin
/Users/mrafayaleem/.virtualenvs/imagemonster/lib/python2.7/plat-mac
/Users/mrafayaleem/.virtualenvs/imagemonster/lib/python2.7/plat-mac/lib-scriptpackages
/Users/mrafayaleem/.virtualenvs/imagemonster/Extras/lib/python
/Users/mrafayaleem/.virtualenvs/imagemonster/lib/python2.7/lib-tk
/Users/mrafayaleem/.virtualenvs/imagemonster/lib/python2.7/lib-old
/Users/mrafayaleem/.virtualenvs/imagemonster/lib/python2.7/lib-dynload
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages
/Users/mrafayaleem/.virtualenvs/imagemonster/lib/python2.7/site-packages
/Users/mrafayaleem/.virtualenvs/imagemonster/lib/python2.7/site-packages/IPython/extensions
/Users/mrafayaleem/.ipython
Can anyone please help me resolve this weird behaviour?
This isn't due to virtualenv. It's because of differences between python and IPython. IPython also imports from the current working directory as well as searching in the PATHs. There's a similar answer in this SO thread .
You should cd
into the imagemonster directory, don't workon
virtualenv, and try:
python server.py
And deal with the import error that throws.
Coming a little too late but this might help others looking for a solution. Ipython uses both your current system path and your current project directory but when running in a virtualenv, only the current PYTHONPATH specified during installation will be used. You have to append your current working directory to your module import declarations:
This is what I would do:
import sys
sys.path.append('/Users/mrafayaleem/Projects/imagemonster/conf')
from imagemonster.conf import settings
The solution here gives a more precise solution especially when working in virtualenv https://stackoverflow.com/questions/19876993/python-module-import-relative-paths-issue
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.