3

I usually run python2 but I am playing with python3. Right now I am confused as to why I am getting this error.

When I run the command ./test_web_events.py in the tests directory, I get:

Traceback (most recent call last): File "./test_web_events.py", line 21, in <module> import qe.util.scratchstore as scratchstore ImportError: No module named 'qe' 

However my project structure has qe directory in it:

/python_lib Makefile /qe __init__.py /tests __init__.py test_web_events.py /util __init__.py scratchstore.py /trinity __init__.py 

I tried moving my /tests directory into /python_lib but I am still getting the same error:

MTVL1289dd026:python_lib bli1$ ls Makefile qe rundata setup.sh tests MTVL1289dd026:python_lib bli1$ python3 tests/test_web_events.py Traceback (most recent call last): File "tests/test_web_events.py", line 21, in <module> import qe.util.scratchstore as scratchstore ImportError: No module named 'qe' 

Here is my sys.path for python2

>>> import sys >>> print sys.path ['', '/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/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/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC', '/Library/Python/2.7/site-packages'] 

sys.path for python3

>>> print(sys.path) ['', '/Library/Frameworks/Python.framework/Versions/3.4/lib/python34.zip', '/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4', '/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/plat-darwin', '/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/lib-dynload', '/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages'] 
5
  • It says up there it is in the test_web_events.py Commented Jan 7, 2015 at 20:48
  • 1
    is there an init.py file in python_lib dir ? Commented Jan 7, 2015 at 20:51
  • @Andrew_Lvov That has nothing to do with it, he isn't trying to import python_lib Commented Jan 7, 2015 at 20:52
  • @mattm, you're right. Commented Jan 7, 2015 at 20:54
  • 1
    Andrew_Lvov meant a __init__.py file (it can be empty). Commented Jan 7, 2015 at 21:06

4 Answers 4

4

The issue is that /python_lib is not in Python path. The behavior is the same on both Python 2 and 3.

In general, do not run scripts from within (inside) a Python package, run them from the top-level directory instead:

/python_lib$ python -m qe.tests.test_web_events 

Thus /python_lib is in Python path and /python_lib/qe/tests is not. It assumes that there is tests/__init__.py file.

Do not modify sys.path manually. It may lead to subtle bugs related to importing modules. There are better alternatives e.g., if you don't want to run the scripts from /python_lib, just install the development version:

(your_virtualenv)/python_lib$ pip install -e . 
Sign up to request clarification or add additional context in comments.

9 Comments

Still getting the same error after I moved /tests into /python_lib =/ dpaste.com/39PBWDC
do you run it from /python_lib? If you print sys.path; you should see this directory in it. Update you question and include the full traceback – you can't get the same error.
yes I did run it in python_lib question is updated!
@Liondancer: It is my mistake. I've removed the broken python tests/test_web_events.py command. It requires something like _preamble.py (in your case search for qe instead of twisted).
Not really sure what this implies =/ MTVL1289dd026:python_lib bli1$ python3 -m qe.tests.test_web_events.py /Library/Frameworks/Python.framework/Versions/3.4/bin/python3: Error while finding spec for 'qe.tests.test_web_events.py' (<class 'AttributeError'>: 'module' object has no attribute '__path__')
|
3

This is most likely because you haven't added /python_lib/qe to your PYTHONPATH.

When you try to import a module, the interpreter will look for it only in a certain number of places, you cannot arbitrarily try to import a module from anywhere.

The most common ways are to have a package installed via pip, to have the module sitting in the same directory as the .py file, or to have added the path to that module to the PYTHONPATH.

See: https://docs.python.org/2/tutorial/modules.html#the-module-search-path

It seems like the latter case is most likely what you want to do. This is going to be dependent on your OS, but googling it should be straightforward.

1 Comment

sys.path is not the PYTHONPATH
2

Make sure you have __init__.py file in all your package folders so that you tructure looks like

/python_lib Makefile /qe /tests test_web_events.py /util __init__.py <------------ create this file scratchstore.py /trinity __init__.py 

and then the you cd to python_lib folder and run ``export PYTHONPATH=`pwd```

1 Comment

Then just fix the PYTHONPATH. by running export PYTHONPATH=/path/to/python_lib.
-1

Just #!/usr/bin/env python add this to your all scripts. Make sure it's on the top.Like;

#!/usr/bin/env python import qe 

I assume you added Python 3 to path, if you did, the only problem is this. You do not need init.py or sys.path or anything. This line already finding Python path automatically if you added Python 3 to path, if Python 2 still on the path then it's normal you got error.

3 Comments

All of them has this? Are you sure?
@Liondancer You are doing something wrong then. I worked with modules a lot, and this was the problem all the time. Make sure your all of scripts has this at the top.
I think it has to do with the python 2 and 3 paths =/ I usually use 2 but i pulled a project and its written in 3. I know I have 3 installed but im not sure how to switch back and forth

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.