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.
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 imported a repo using git and when I try to run a file, it shows that "it could not be resolved".
The running script has:
import craft_utils
import test
import imgproc
import file_utils
The imported things are all script.
I have attached the screenshots for the error and hierarchy.
How can I solve this? Thanks.
try adding an empty file named __init__.py in the root folder CRAFT-pytorch (instead of the basenet folder)
create a empty file named __init__.py
it would solve the issue
Add a file inside of the folder called __init__.py
First you need to create an empty file named __init__.py in the folder CRAFT-pytorch
Second add these line at the top of the file you're trying to running, before the imports:
import os
import sys
import inspect
app_path = inspect.getfile(inspect.currentframe())
main_dir = os.path.realpath(os.path.dirname(app_path))
sys.path.insert(0, main_dir)
import craft_utils
import test
import file_utils
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
I have my project structure like this:
/application
/DIR1
__init__.py
file_that_import.py
file2.py
/DIR2
__init__.py
file_tobe_import.py
__init__.py
How should I import the file that is inside DIR2 from outside the directory?
You'll want to add the paths to the import path list like so
import sys
sys.path.append("/your/path")
Then import from those directories
Right,
I have a structure like so:
/Project
__init__.py
script_a.py
/tests
__init__.py
test_something.py
When test_something tries to import using from . import script_a it returns the error Attempted relative import in non-package.
I've put in the empty __init__.py files and added /Project to PYTHONPATH but it still throws up this error!
Any ideas?
EDIT:
I have now used a tester.py situated in \Project and call:
import script_a
from tests.test_something import *
Now it works!!
When test_something tries to import using from . import script_a it returns the error Attempted relative import in non-package.
Using one dot . will lead you to the current directory. You should use two dots .. to get to the parent dir.
You cannot run a module with a relative import itself. You can only import it. So test_something.py can only run as an import but it is not possible to run the script as __main__
from Project import script_a should work for you.