2

although there are many posts on the internet as well as some posts on stack overflow, I still want to ask about this nasty python "import" problem.

OK. so, the open source code organization is usually like this:

project/src/model.py;

project/test/testmodel.py

if I put the famous __init__.py in project directory and also in src/ and test/ subdirectories, and then put "from project.src import model" for the testmodel.py. it does not work! keep telling me that the Module named "project.src" is not found!

how can I solve the problem without changing the code structure?

4 Answers 4

3

You shoud not add the project directory to your pythonpath but it's parent, e.g. imagine the setup

/home/user/develop/project/src/model

You'd add /home/user/develop to PYTHONPATH

If that still doesn't work, make sure you don't have a 'project.py' insite project/src/model.

Sign up to request clarification or add additional context in comments.

Comments

2

Make sure you have the parent directory of project/ on your pythonpath, rather than the project directory. If you add the project path itself, imports like import project.src will look for project/project/src.

Comments

0

The directory where project is located is probably not in your python path.

1 Comment

I added the project directory into the pythonpath. the problem remains.
0

You can use a relative import (assuming Python 2.5+) from testmodel.py, like:

from ..src import model

However, this does not work if you're running testmodel.py as the main module.

Comments

Your Answer

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.