0

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?

Rafay
  • 6,108
  • 11
  • 51
  • 71
  • Do you have a file called __ init __.py in your inner imagemonster directory? (Remove the spaces, just adding those so SO doesn't interpret underlines as formatting) – dylrei Jan 17 '15 at 16:49
  • @dylrei Yes! For sure! – Rafay Jan 17 '15 at 16:50
  • Running `python imagemonster/server.py` will treat it as a script, not a module, so it won't see it as being inside a package. `python -m imagemonster.server` might work. – Thomas K Jan 17 '15 at 23:35

2 Answers2

0

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.

Community
  • 1
  • 1
RedCraig
  • 503
  • 1
  • 4
  • 15
0

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

Community
  • 1
  • 1
Gadosey P.
  • 85
  • 9