import module inside the module in python - python

Python 3.10
I have a project structure like so:
./main.py
models/__init__.py
models/file1.py
models/file2.py
models/file3.py
main.py:
from models import file1, file2, file3
models/file1.py:
from models import file2
models/file2.py:
from models import file3
when I execute main.py, so far so good. But the fact is that I would like to execute file1.py as a standalone script as well like:
python file1.py
but now, python cannot find the models package since it is in it!
My work around is to append ./models in sys.path from main.py and just:
import file2
in file1.py, but it is not really satisfying, I would like to continue importing file2 with the statement
from models import file2
since it seems more pythonic and more 'portable'.
How to do so?

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

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.

Transform a python folder into a package: `ModuleNotFoundError`

Suppose that in a folder my_package, i have three files :
__init__.py, blanck
file1.py, containing a functon function1
file2.py, containing the import statement : from file1 import function1
Then, from another directory, when I use import my_package.file2 as file2, I have a ModuleNotFoundError coming from the line of the statement from file1 import function1.
Obviously, i did something wrong somewhere. But where ? The MWE i gave is small, but i append to have a lot of python files in my_package folder, some of them importing each other. I'm trying to change this directory into a package that i can import from elsewhere, that's why i added the __init__.py blanck file, but it does not seem to work that way.
Check this tree structure.
my_package
|
'----- __init__.py
'----- file1.py
'----- file2.py
Script (somewhere in your system)
|
'--- test.py
when you import my_package (import my_package.file2 as file2) in the another directory say in test.py, it looks for the directory named my_package in its current path.. (i.e. inside Script folder )
as my_package is not present inside the Script folder you will get the error ModuleNotFoundError.
so in test.py write the code as shown below
import sys
sys.path.append("/home/MyFiles/my_package") # absolute path of the my_package folder
import file2
# Now use file2 and work on it
# all other modules/files in my_package folder can also be imported.
I suppose i found the solution myself : I added a setup.py file with propper content to the folder and then i ran pip -m my_package, allowing me to import it from everywhere..

Importing files in different directories in Python

I have a directory structure like this:
dir/
frontend.py
dir1/main.py
dir2/backend.py
How do I import backend in main in Python?
How do I import frontend in main in Python?
Have tried all the answers on Stackoverflow. Nothing seems to work.
In any folder from which you want to import source files you need to have existing init.py file.
I would advise structure like:
dir/
main.py
dir1/frontend.py
dir1/__init__.py
dir2/backend.py
dir2/__init__.py
Then you import them in following fashion (in main.py):
import dir1.frontend
import dir2.backend
There is only one rule when it comes to importing files in a Python project.
You have to import the package relative to the directory from where the project is run.
For example in the question main.py should have something like this:
from dir.frontend import *
from dir.dir2.backend import *
But then you'll have to have something like main.py under dir/ which imports dir/dir1/main.py and then run python main.py.
So, try to keep the main.py always in the head directory so you don't have to worry about such a situation as above.
ONLY ONE RULE: Everything has to be imported relatively to the directory from where the project is run.

Change python path of imported module

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.

Categories