Python 3 not reading path of file - ModuleNotFoundError - python

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.

Related

Why can't I import two Python files on the same level the same way?

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

ModuleNotFoundError: Even with __init__.py file

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

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

ModuleNotFoundError: No module named 'metadata_setup'

I have a directory structure like the following:
/
/setup
/sqlalchemy
__init__.py
metadata_setup.py
/server
/data
__init__.py
simulations.py
In simulations.py I have:
import sys
sys.path.insert(0, '/setup/sqlalchemy')
import metadata_setup
but then I get the following error at the import statement:
ModuleNotFoundError: No module named 'metadata_setup'
I tried adding a __init__.py to the root directory but I'm still getting the same results. I'm running Python 3
Either insert full path or edit relative path like:
sys.path.insert(0, '../../../setup/sqlalchemy')
This worked for me:
import sys
sys.path.insert(0, os.path.join(os.path.dirname(sys.path[0]),'setup', 'sqlalchemy'))
import metadata_setup

Import module error

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

Categories