newbie question: how to run a python file if it is in a package

N

neoedmund

for example:

X.py is in aaa.bbb and it has a line like "import aaa.bbb.Y"
how can I run X.py avoiding it saying such like "ImportError: No
module named aaa.bbb"?

Is all runnable script must be in the default package?

thanks.
 
D

Diez B. Roggisch

neoedmund said:
for example:

X.py is in aaa.bbb and it has a line like "import aaa.bbb.Y"
how can I run X.py avoiding it saying such like "ImportError: No
module named aaa.bbb"?

Is all runnable script must be in the default package?

There is no such thing as a "default package"

All imports are resolved over the "sys.path" list of directories (or eggs).

There are various ways to modify this:


- by hand. If you know X.py lives below aaa/bbb, you can get it's
__file__-attribute, and walk the way down two levels. Then add the
resulting path to sys.path. All this has to be done before any
import of aaa.bbb occurs.

- modify the environment variable PYTHONPATH to contain the root of
aaa.bbb before starting the script.

- write a custom .pth-file and place it into your pythno installation.
The pth-file will contain the above mentioned root path.

- use setuptools to create script entry-points, and install your whole
application either fully, or as so-called egg-link. Possibly inside
a virtualenv. This would be my personally preferred method.

- simply copy your wohe aaa.bbb-stuff into site-packages. Make sure you
do that whenever you change something in your code.


Diez
 
N

neoedmund

There is no such thing as a "default package"

All imports are resolved over the "sys.path" list of directories (or eggs).

There are various ways to modify this:

  - by hand. If you know X.py lives below aaa/bbb, you can get it's
    __file__-attribute, and walk the way down two levels. Then add the
    resulting path to sys.path. All this has to be done before any
    import of aaa.bbb occurs.

  - modify the environment variable PYTHONPATH to contain the root of
    aaa.bbb before starting the script.

  - write a custom .pth-file and place it into your pythno installation..
    The pth-file will contain the above mentioned root path.

  - use setuptools to create script entry-points, and install your whole
    application either fully, or as so-called egg-link. Possibly inside
    a virtualenv. This would be my personally preferred method.

  - simply copy your wohe aaa.bbb-stuff into site-packages. Make sure you
    do that whenever you change something in your code.

Diez

thanks Diez, i think your answer is almost perfect.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,691
Members
48,796
Latest member
Greg L.

Latest Threads

Top