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
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 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.
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.
My directory structure is:
[File1]
[Package1]
__init__.py
from Package1 import module1
from Package1 import module2
...
module1.py
module2.py
...
I want to import a package so that using a class like
from File1 import Package1
Package1.Module1.Class1()…
is possible.
When I try
from File1 import Package1
I always get the error:
cannot import name Package1
I think that Circular imports may be the problem, but I don't know how to fix it.
1) You need to add __init__() in File1 folder also (empty also ok).
2) Change __init__() inside Package1 as follows:
__init__.py
from File1.Package1 import module1
from File1.Package1 import module2
Then from your python file you can access like
from File1 import Package1
x=Package1.module1()
x=Package1.module2()
I think this will work....
have fun