1

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?

2 Answers 2

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

and then call prog.py while in directory b.

Sign up to request clarification or add additional context in comments.

Comments

4

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"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.