Problem with import

V

Victor Lin

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.
 

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

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,817
Latest member
AdalbertoT

Latest Threads

Top