Reputation: 525
Lets say I have a directory tree that looks like this:
main -
|
lib-
|
core-
|
fun-
|
some_file
stuff-
|
another_file
How could I import the modules from some_file
into another_file
? Everytime I try to do the importing (yes I know about __init__.py
) I get an error:
Traceback (most recent call last):
File "file.py", line 6, in <module>
from some_file import some_method
ImportError: No module named some_file
Is it possible to import the modules into another file?
Upvotes: 0
Views: 2216
Reputation: 984
Just add an __init__.py
file to the directory to make it be seen as a module:
main -
|
lib-
|
core-
|
fun-
|
some_file
__init__.py
stuff-
|
another_file
The __init__.py
can be a blank file, all that matters is that it exists. Then, you can do import fun.some_file
Upvotes: 0
Reputation: 7330
You can import using absolute or relative imports if all the directories that you're traversing are Python packages (with __init__.py
file in them).
Assuming that you are running your program from the directory in which main
package is, you'd import some_file
module with:
import main.lib.core.fun.some_file
Otherwise you have to append to Python path before attempting import:
import sys
sys.path.append("......main/lib/core")
import fun.some_file
The second example assumes that fun is a Python package with __init__.py
file in it.
Upvotes: 1