Problem with import

Victor Lin Bornstub at gmail.com
Mon Feb 11 07:33:11 EST 2008


Hi,

I got some problem with import. I have some python file that should be
executed. Something like unit-test or other small tool. The project is
not so small. So I want to separate these files into different
packages.

Might like this:

project/
----run_task.py
----pkg1/
--------__init__.py
--------run_lettle_task.py
--------module_a.py
--------module_b.py
--------test1.py
----pkg2/
--------__init__.py
--------module_c.py
--------module_d.py
--------test2.py

Now here comes the problem: How to import these file correctly?
For example:

# file module_a.py ---------------------

# This import force who run and import this file most in project/ this
directory
import pkg2.module_c

# file test1.py ----------------------------

# oops! If we run this file,
# we got a error here, because this file is not in project/
import module_c

# do some unittest
-------------------------------------------------

As you see, because I use import like this "import pkg2.module_c".
This statement force python files which want to import and use this
module should be in project. But I don't want to put all the files
needed to execute in project. Because there are so many files and
tests need to execute. If I put so many different things in project,
to separate files to packages would be meaningless. What can I do?

Actually, I know I can modify path to make python to search the
project directory. Then in test1 or test2 I can import module_c. But
here comes another problem: If I move my directory, I have to modify
the path again. This is not a library, they are some programs to do
specific task. I think they should be executable in every where I move
or copy the directory to.

Victor Lin.



More information about the Python-list mailing list