Error ImportError: No module named "apparently not related to my code

I try to exercise 47 Learn Python The Hard Way, 2nd Edition, but I get this error when I run tests/ex47_tests.py :

 File "tests/ex47_tests.py", line 3, in <module> from ex47.game import Room ImportError: No module named ex47.game 

I thought this was what I was doing wrong in my code because I am very new to this, so I cloned this repo from a GitHub user who seems to have successfully completed the exercise.

Not only the corresponding parts of our code are identical, but I get the same error when I try to run tests/ex47_tests.py , which I cloned from him. So now I am lost and hope someone has a solution for me. Any ideas?

+4
source share
6 answers

from the repository directory:

 PYTHONPATH=. python tests/ex47_tests.py 

Make sure there are no other ex47.py files / packages in your path.

+3
source

FabrizioM's answer should make it work. Here is a little explanation.

When Python downloads a file, it searches the file system. So we have an import statement:

 from ex47.game import Room 

It searches for the ex47.py file in the module search path (accessible as sys.path in Python code). The module search path contains some directories based on the Python installation details, directories listed in the PYTHONPATH environment variable, and contains the parent directory of the script you are running. He does not find ex47.py in the path, but he sees that inside it there is a directory named ex47 with __init__.py . He then finds game.py in this folder.

The problem is that your current folder is not in the module search path. Since ex47_tests.py has been started, it has $ cwd / tests on the way. You need $ cwd on the way.

 PYTHONPATH=. python tests/ex47_tests.py 

does just that. It puts $ cwd in the module search path, so Python can find the source files.

You can also do:

 python -m tests.ex47_tests 

This will work as a module, not a file, while it will use the current directory as the path that it automatically adds to the module search path, and not the directory in which the file is located.

+5
source

In the book you are invited to copy the catalog "skeleton", and then use it for exercises in the game room (No. 47). The skeleton catalog has a directory of "NAME".

So, if you copied the skeleton directory and named it ex47, it will have another ex47 directory in it.

 lpthw > ex47 > ls bin docs ex47 setup.py tests 

So, when the book says, β€œThen create a simple ex47 / game.py file where you can put the code to check,” you assume this is the top level of ex47. Not true! Therefore, imports will not be allowed.

I also got into the same problem until I realized that the book calls the external directory (the one we got by copying the "skeleton") as a "simple game", obvious from this line in the book -

 ~/projects/simplegame $ nosetests 

So, all the answers about PYTHONPATH are valid, I just wanted to explain why the import will not work for you!

+3
source

There is something wrong with the directory structure. Here is my directory structure:

 bin docs ex47 setup.py tests ./bin: ./docs: ./ex47: game.py game.pyc __init__.py __init__.pyc ./tests: ex47_tests.py ex47_tests.pyc __init__.py __init__.pyc 
+1
source

The project is named ex47, and it has a folder named ex47. (assuming that you named your files according to his plan outlined before game.py in this exercise) When the author says that he creates a simple ex47 / game.py file, it means that the game.py file is inside the folder to be created and saved.

0
source

I also had the same error; being new to python, I followed the example of a book and after copying the skeleton directory, renamed any files containing "NAME" - one of them existed in the test / directory, and after deleting the remote boot the error disappeared - check the response / contents in the Microivan message in the directory above. Therefore, only two python files, game.py and ex47_tests.py should exist in the project directory - I hope you got there before the next exercise. !!

-1
source

All Articles