I have a directory structure like this
-project
--folder1
---file1.py
--folder2
---file2.py
How can I import a function from file2 and use it in file1?
I tried to
import sys
sys.path.insert(0, '../folder2/')
from file2 import foo
foo()
and this doesn't work.
I tried to include __init__.py in each folder at it didn't work either.
Do I have an error with the relative path, or what should I do?
For a simple solution you can add the "absolute" path to the project directory and then put from folder2.file2 import foo in file1.py like :
import sys
sys.path.insert(0, <absolute path to "project" direcotry>)
from folder2.file2 import foo
foo()
now you can run your file1.py directly, python will find folder2 and therefore file2.
Alternatively you can add the "absolute" path to the folder2 directory and then put from file2 import foo in file1.py like:
import sys
sys.path.insert(0, <absolute path to "project" folder2>)
from file2 import foo
foo()
now you can run your file1.py directly, python will find file2.
use import folder2.file2 or from folder2.file2 import function_name
PS: Your project directory should be in python path
Related
I want to import file1.py and file2.py, which are both on the same level. I used the same code to import file1.py and file2.py but am still getting a modulenotfound error for file2.py while importing file1.py works fine.
The file structure is like this:
current_folder -> current_file -> import_folder -> file1.py, file2.py
I want to import file1.py and file2.py in current_file.
I imported sys and added:
sys.path.append(os.path.join(sys.path[0], '../'))
sys.path.insert(0, 'C:\\Users\\Bob\\current_folder\\import_folder')
import file1
import file2
However, I keep getting a modulenotfound error about file2.py.
You shouldn't need to modify your path at all. When you execute current_file, current_folder will be added to your path automatically, which means you can refer to (the package) import_folder directly. (You may need to add import_folder/__init__.py to make it work.)
import import_folder.file1
import import_folder.file2
Here is the Project structure -
MAIN Project FOLDER:
file1.py
Directory1
testFile.py
file1.py is in the Main Project folder.
testFile.py is under Directory1.
I need to import file1.py into testFile.py.
If I just add
import file1 in testFile.py, it gives me error - ModuleNotFoundError: No module named 'file1'
You need to add the path to the sys.path in testFile.py, can you try this:
import sys
sys.path.append(path.join(path.dirname(__file__), '../'))
import file1
Hope this helps.
I have a project like:
project/
foo/
other.py
bar/
baz.py
If I code something like:
import sys
sys.path.insert(0, '../foo')
from foo import other
It doesn't work. But if I move foo into the bar directory it works. I realize this is bad code style, but how is this relative path searched and where does the documentation define it?
from foo import other will append foo to each of the directories in sys.path. So it's looking for ../foo/foo/other.py, but the actual path is just ../foo/other.py.
You should just insert .. into sys.path, then it will look for ../foo/other.py. Or if you only want to include that directory in the path, just use import other, without from foo.
Assuming you are in the baz directory, you need to include your parent directory .. in the path. Then when you do from foo, the foo directory in your parent directory is found.
This works:
import sys
sys.path.insert(0, '..')
from foo import other
Read more:
Stackoverflow Answer
The Definitive Guide to Python import Statements
Alternatively, since you included ../foo in the path, you could simply do import other, like so:
import sys
sys.path.insert(0, '..\foo')
import other
I have the following file structure:
And I don't know why this piece of code doesn't work:
# this is in main.py
import sys
from pathlib import Path
main = Path(__file__).parents[2]
if main not in sys.path:
sys.path.insert(0, main)
print(main)
from mainFolder.dir1.subdir1 import forImport
Comparing it to this Import module from subfolder I can't explain myself what I am doing wrong.
Thank you in advance!
your main.py is within a subfolder, the import statement doesn't search "siblings" of that folder, only scripts in the folder where main.py is and subfolders in the same folder
if your main.py was in the folder above mainFolder your statement would work
I have these files on my computer:
some/random/path/config.py
my/path/defined/here/file1.py
my/other/path/here/file2.py
Then, I have:
#File: config.py
import sys
path1="my/path/defined/here/"
sys.path.append(path1)
path2="my/other/path/here"
sys.path.append(path2)
import file1
and,
#File: file1.py
import file2
file2.runFunction()
The path "my/path/defined/here/" is defined in config.py, but I would like to somehow add that same path to file1.py so that I can import file2.py in file1.py. I would like to be able to do this without having the exact path in file1.py as I will have multiple config.py py files.