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.
Related
I have a folder named as "model_dir" contains multiple *.py files.
model_dir:
"model.py"
"utils.py"
inside model.py I can import like this: from utils import test
I want to copy the whole folder inside a package. Currently to make the import works inside the package I have two options:
1- adding folder path to sys.path
2- add dot for imports, like this: from .utils import test
Is there any way to make imports work without changing the code or sys path?
Have you tried including a blank __init__.py file in the model_dir folder? That should let python files import sibling files- I'm a novice dev myself and often run into pitfalls with imports inside packages, and I've found this link
to be pretty helpful figuring out what I've done wrong
In the current working directory , i have following structure
Project
__init__.py
-RestApi
__init__.py
app.py
query_generator
-testfolder
__init__.py
test1.py
I want to call query_generator from test1.py , I tried calling
from . RestApi.query_generator import *
but getting following error
ImportError: attempted relative import with no known parent package
This question might be duplicate of following Importing files from different folder , Python relative-import script two levels up, Attempted relative import with no known parent package . But I am not able to solve it for my current problem
Try using below import:
from Project.RestApi.query_generator import *
There are multiple ways to achieve this.
you can add path till Project dir in your PYTHONPATH variable
export PYTHONPATH=$PYTHONPATH:<path_leading_to_Project>/Project
Then inside test1.py you can import the query_generator module using:
from RestApi.query_generator import *
Advantage of doing in such a way is if you execute your script from any working directory, it will work
I am working on some python project (2.7) and I have issue with imports. When I run main.py it start scripts from tests folder etc. and save output to logs and everything works fine.
/root------------
-logs
-staticCfg
-config.py
-tests
-systemTest
-scrypt1.py
-scrypt2.py
-userTest
-uScrypt1.py
main.py
My static variables (email, name etc.) are located in config.py. I need to import config.py in scrypt1.py or scrypt2.py. I tryed adding __init__.py to tests, systemTest and staticCfg folder but I always get an error.
In my scrypt1.py:
import staticCfg as cfg
...
or
from staticCfg import *
...
I get the error:
ImportError: No module named staticCfg
The import mechanism of Python can be a bit tricky.
You can refer to the documentation for more information: Python Import Mechanism
When you use absolute imports (your import does not start with a .) as you do, the import path will start from your main script (the one you launch). In your case, it's scrypt1.py. So starting from this location, python can't find the package staticCfg.
For me, the simplest solution is to create a main script in your root directory and call scrypt1.py from there (imported using from tests.systemTet import scrypt1.py). In this case, the base package will be your root folder and you will have access to the package staticCfg from all your script files as you wanted to do.
you may add root folder to PYTHONPATH.
I'm working on a Flask app and run into issues with trying to run a script within a module where the script is in a different directory. I've tried looking at several solutions here and on other sites and haven't been able to find something that works. I have a project structure like so:
dashboard\
app\
static\
templates\
__init__.py
jobs.py
api_fetch.py
config.py
run.py
Within jobs.py I have a function that needs to run api_fetch.py but for the life of me, I'm not sure what I need to do to that. I've tried imports with .., sys, os and nothings worked. This seems like it shouldn't be that difficult, but I'm at a loss. So far I've only needed to import modules on the same path which work fine.
in jobs.py:
from .. import api_fetch
then 'outside' dashboard:
$ python -m dashboard.app.jobs
__init__.py should be in dashboard also.
You can just import from parent package dashboard, the file structure should like this:
dashboard\
|app\
|static\
|templates\
-__init__.py
-jobs.py
-__init__.py << don't forget this!
-api_fetch.py
-config.py
-run.py
Then in jobs.py:
from dashboard import api_fench
Sorry for asking my own question 2nd time, but i am totally stuck in import file in python.
I have a directory structure below:
|--test/foo.py
|--library #This is my PYTHONPATH
|--|--script1.py
|--|--library_1
|--|--|--script2.py
|--|--library_2
|--|--library_3
I am accessing library/library_1/script2.py from test/foo.py.
Here i am confused about what is the better approach. Generally all library folders or utility functions should be added to pythonpath.
This is a folder structure i am maintaining to differentiate utility functions and test scripts.
I tried putting __init__.py in library and library1 & then imported like from library1 import script2, but getting error as No module named script.
I have tried appending that path to system path as well.
Working: if i add another pythonpath like path/to/library/libray_1/. So should i do this for all folders which are inside library folder to make it work ?
Here's what you need to do:
|--test/foo.py
|--library #This is my PYTHONPATH
|--__init__.py
|--|--script1.py
|--|--library_1
|--|--|--__init__.py
|--|--|--script2.py
|--|--library_2
|--|--|--__init__.py
|--|--library_3
|--|--|--__init__.py
And inside the first __init__.py below library you need to do:
import library1
import library2
import script
Then, if library is your python path, you can do this within test/foo.py with no errors:
import library
library.library1.bar()
library.script.foo()