Reputation: 471
I seem to be running into this issue intermittently.
Sometimes I have a PyDev project in which I get compile errors if I try to start my imports with the name of the top-level folder. So, suppose my project looks like this:
+ myproject
- __init__.py
- a.py
- b.py
+ package1
- c.py
- __init__.py
+ package2
- d.py
- e.py
- __init__.py
If I'm in file a.py, it won't let me do imports like this, for example:
from myproject.b import foo
import myproject.b
The same goes for any file. They all compile just fine if I leave off "myproject" from the imports statement like this:
from b import foo
import b
Just like in the diagram, I definitely have a top-level __init__.py, and both the myproject folder and its parent are on the Python path. The myproject folder is also the source folder for the project.
I need the complete import statement because places I'm deploying this code to won't have the myproject folder on their path. Some other projects that seem to be configured the same way don't have this problem, and I've tried the usual cleaning/restarting.
Any idea as to what's going on here?
Thanks!
Upvotes: 1
Views: 521
Reputation: 471
So, after tinkering a bit, it turns out that PyDev won't let me start an import statement with a package that itself is on the Python path. If I remove the source folder designation from my project folder, the imports work fine, but then of course I lose some of PyDev's functionality.
So, my new problem is slightly different, so I've posted a different question here:
https://stackoverflow.com/questions/18613944/how-should-i-structure-my-pydev-git-project
Upvotes: 1
Reputation: 2997
Have you tried relative imports (from .b import foo
, from . import b
)? I don't understand why absolute imports (including "myproject") doesn't work in your case but meanwhile relative imports should still work.
Upvotes: 1