niteshb
niteshb

Reputation: 2829

importing from subdirectories in Python

I am new to Python and I am confused in importing files from subdirectories. My filesystem structure is as follows:

/doc/a/main.py

      /second.py

/doc/b/prog.py

Now I want to import main and second in prog.py. Can anyone suggest me a method to do so?

Upvotes: 1

Views: 9538

Answers (2)

user2665694
user2665694

Reputation:

First you should learn what a proper Python package makes: it needs to contain a file named __init__.py

The search path of Python can be configured either by modifying sys.path or setting the $PYTHONPATH environment variable.

See also

http://docs.python.org/tutorial/modules.html

or google for "python import pythonpath"

Upvotes: 4

Sven Marnach
Sven Marnach

Reputation: 601321

import sys
sys.path.append("../a")
import main, second

and then call prog.py while in directory b.

Upvotes: 6

Related Questions