I have seen a lot of answers over this simple problem but I can't find a good solution for such, even a workable solution; the problem is, I have this proyect structure
parent_folder
|
|__folder1
| |_____ __init__.py
| |_____ file1.py
|
|__folder2
|_____ __init__.py
|_____file2.py
I want to call a function (lets name it function2) from file2.py inside file1.py, but when doing an import like this:
# inside file1.py
from folder2.file import function2
I get the following error:
ModuleNotFoundError: No module named 'folder2'
Then I tried using absolute import, so the code looked like:
# inside file1.py
from ..folder2.file2 import function2
And I get the following error:
ImportError: attempted relative import with no known parent package
And finally I tried adding such directory to my path using the following code:
#inside file1.py
import sys
sys.path.append("..")
from ..folder2.file2 import function2
And still, get the same error:
ImportError: attempted relative import with no known parent package
What is the definitive CLEAN solution for such problem?,I would appreciate an answer, thanks
NOTE: My current Python version is 3.8 and the code is running under Windows 10
file1.py
import os
import sys
sys.path.append(os.path.dirname(os.getcwd()))
from folder2.file2 import function2
Related
I have 3 python file :
test.py
modules(folder) and in modules there 3 to files : module1.py module2.py init.py
test.py
./test.py
./modules
./modules/module1.py
./modules/module2.py
./modules/__init__.py
module1.py:
from module2 import temp
def print_temp():
print(temp)
if __name__=='__main__':
print_temp()
module2.py
temp =1
test.py
from modules.module1 import print_temp
print_temp()
When I run python test.py I got ModuleNotFoundError: No module named 'module2'
How can I fix that please?
When you import a module in Python, it searches the PYTHONPATH for a module / package with that name. But it doesn't search inside directories so you have to specify.
If you want to be able to run modules.module1 directly (as well as import it) then you must use the full path:
from modules.module2 import temp
But if you just want to be able to import the module and not run it directly it is better to use a relative import as it will still work even if the name of the package is changed:
from .module2 import temp
The following is my folder structure:
/experiments
/experiment_1
/experiment_2
/experiment_3
/src
/sample.py
helper.py
All experiments share some code, which I have exported into helper.py. Now I'm trying to import a function from helper.py into sample.py:
from ...helper import my_function
and I get the following error:
ImportError: attempted relative import with no known parent package
Please note that I have tried many solutions offered here, but none worked.
create a __init__.py file in your parent folder.
The above question is related to
Importing files from different folder
The solution is to add the following lines in the sample.py:
import sys
sys.path.insert(1, 'path-to-experiments')
from helper import get_json
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.
I have a custom python module I'm working on and am confused how I should import modules into other modules. I want to use bits and pieces of some modules within others and keep getting an error: ImportError: cannot import name NameOfModule
I'm assuming there's some sort of circular reference that's causing the issue but I'm not sure if I need to add something to __init__.py, or if there's a specific way of importing the modules into each other, or if I should change my folder structure?
If I want to be able to use some function from mod1.py within mod2.py how should I go about setting up the import statements?
My current folder structure is:
FolderName
-__init__.py
-mod1.py
-mod2.py
-mod3.py
-mod4.py
Sample code:
__init__.py is empty
mod1.py: from . import mod2
mod2.py: from . import mod1
You should be using relative imports for files within the current module, like so:
from . import mod2
Or:
from .mod2 import foo
And unless you have a VERY good reason, you should be using Python 3.
I'm having difficultly with important a python module from another folder. Here's how my folder looks currently
foldername/
__init__.py
A/
__init__.py
spam.py
grok.py
B/
__init__.py
foo.py
I'm trying to import the functions and classes from the grok.py file into the foo.py in B. This is how my foo.py looks like
from ..A.spam import func
However, I get the following error:
ValueError: attempted relative import beyond top-level package
Could somebody help me? I don't understand where I'm going wrong
You can't use '..' like you do on the command line. You have to add your 'A' folder to your Python path. You can use sys.path.append('/dir/of/A') and then from A.spam import func
Instead of using sys.path you could also add a *.pth-file to your python or anaconda "site-packages"-folder which contains the path to the folder "A".
Import via from A.spam import func as #bikemule already proposed.