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.
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
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
i have a file path that looks like this:
project_folder
|--sub_folder
| |--file1.py
|--file2.py
now in file1.py i would like to import file2.py but my attempts don`t work:
#this is what i tryed in file1.py:
from . import file2.py
from .. import file2.py
from ...project_folder import file2.py
from project_folder import file2.py
import ..file2.py
import project_folder.file2.py
everything is failing with: "ImportError: attempted relative import with no known parent package".
I`ve also tried to add a __init__.py file to the project_folder and the sub_folder but that did not help.
I`m not shure if anything has to be in these __init__.py files or if they can stay empty.
for me the answer is: Just import the files that are inside and not outside a folder. If you import a file that is outside the folder it can break you Programm because you created an import loop.
So if you think you have to do a relative import you might have to rethink your file system.
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.
The directory structure is as follows:
folder1
__init__.py
file1.py
folder2
file2.py
If I write file2.py as follows:
from folder1 import file1
I get the error No module named 'folder1'.
If I write file2.py as follows:
from ..folder1 import file1
I get the error ValueError: attempted relative import beyond top-level package.
How can I import file1 from file2?
Try the following:
import sys
sys.path.append("..")
from .. import file1