I am currently having a problem with python packaging and references to that.
My structure is as follows:
code/
package/
A/
__init__.py
a.py
aa.py
B/
__init__.py
b.py
bb.py
C/
__init__.py
b.py
bb.py
__init__.py #1
documentation/
...
other_stuff/
...
(All __init__.py
are empty)
According to everything I have read, I should be able to reference and import things like this (in a.py):
from package.B.bb import whatever
However, this does not work. When I duplicate the outer __init__.py
to the 'code' folder, I can import things like this, however:
from code.package.B.bb import whatever
This is obviously non-ideal for most actual uses.
What is it I can do to achieve my target behaviour? (I'm assuming it's something simple which I am just missing)
(Some more details: I am using Python 2.7 and PyCharm 4.03)
You have the parent directory of code
listed on sys.path
, but you need to have the code
directory *itselfadded to
sys.path`.
In other words, you need to have /full/path/for/code
in sys.path
, not just /full/path/for
.
Note that Python automatically adds the current working directory or the parent directory of a script to sys.path
; see the various options listed in the Command Line Interface Options documentation.
For example, a Python script located inside code
, when run with python path/for/code/script.py
will have the code
directory added to sys.path
for that run.