Import module error - python

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

Related

Import script from another script in python

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

Python ModuleNotFoundError while importing from another directory

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

how to make relativ or absolute imports work

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.

Python 3 not reading path of file - ModuleNotFoundError

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.

python unittest failing with relative import in source code

I have my python source code and unittest code structure in the following hierarchy:
bin/
module1.py
module2.py
module3.py
test/
module1_test.py
The code in the modules are as follows:
module1.py
from module2 import testMethod1, testMethod2
def testMethodY():
...
...
...
module2.py
from module3 import testMethod4
module3.py
def testMethod4():
module1_test.py
import unittest
import sys, os
path = os.path.dirname(__file__)
path = os.path.join(path, 'bin')
sys.path.append("/bin")
from module1 import testMethodY
...(Some code for unit test)
when I run python module1_test.py
it fails with import error:
ImportError: cannot import name testMethod4
Not sure what is wrong. If I run the source code then all the imports are working fine. But with python unittest these relative imports are failing. Can anyone tell me what mistake i'm doing.
Create an __init__.py module in your tests folder and add the following to it:
import sys, os
path = os.path.dirname(__file__)
path = os.path.join(path, 'bin')
if path not in sys.path:
sys.path.append(path)
I think your path append statement is incorrect. You are appending /bin which would look for bin in the disk root.
sys.path.append("/usr/lib/bin")
The issue was because of another module with the same name coming from python libs. I renamed it and that solved the issue. Another approach that helped is setting the module to PYTHONPATH.

Categories