I am currently trying to import some python files inside of a folder named "modules". My file structure is as follows:
src
- classes
- modules
- image generator ( the file where I am trying to import modules)
Error:
from modules.processing import StableDiffusionProcessingTxt2Img, process_images
ModuleNotFoundError: No module named 'modules'
from modules.processing import StableDiffusionProcessingTxt2Img, process_images
I have attempted to add both an __init__.py in my classes folder, as well as my modules folder, but unfortunately that did not resolve any of my problems.
I have verified that the files I am trying to import are infact in my modules folder.
You must import from the projects' root directory. Assuming src/ is the root directory, you must import as follows:
from classes.modules.processing import StableDiffusionProcessingTxt2Img, process_images
Since you are trying to import functions from another python file that's located on another folder, it is having a hard time figuring out where it is located.
Try this:
import sys
sys.path.insert(1,'/src/classes/modules')
import (your python file)
Make sure you keep the above code on the python file where you are trying to import functions from other .py files
Remember, the path is the absolute path from root. Not sure how many more layers you have before src, put accordingly.
Also, the last line is import python file. So, if you have x.py inside modules, try import x. Then you will be able to use functions inside there.
Related
I am working on a project based on Python 2.7 and I am trying to import a module which is in a package folder that contains __init__.py and the file that I want to import called package1.py, but I am unable to do so. This is my folder structure: main_project/Tools/common/package1.py
Note that my project files are in the folder main_project. So, I am trying to call the package1.py by using an import statement in my script:
from Tools.common.package1 import variable
But I am getting an ImportError: No module named Tools.common.package1.
What is the solution to solving this error as I want to use the package feature for my project?
Maybe use the solution i found here :
# some_file.py
import sys
# insert at 1, 0 is the script path (or '' in REPL)
sys.path.insert(1, '/path/to/application/app/folder')
import file
or verify your module has a __init__.py
Importing files from different folder
Ok I have found the answer. I also had to insert an init.py in the folder Tools as well. Initially I only inserted init.py in common but not in Tools as we should also make Tools a package if we want to access common
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.
Anyone knows how to import an entire script from another folder without using os ? specifically I want to import the script utils_database.py in basic_etl.py I tried to use from utils import utils_database in basic_etl.py but it does not work.
Thanks!
The root folder of your project is one level up from the utils folder, so you have to include 01.etl as the top-level package, but then 01.etl is not a valid package name as it starts with a number, so you should rename the folder to something like etl first and then do from etl.utils import utils_database in base_etl.py.
I have a problem with python directories.
I have structure like:
python
modules
algorithms
cyphers
morse.py
tests
algorithms
cyphers
test_morse.py
When I try to import module in test I get a module not found error.
I am trying to import like:
parent_dir_name = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
sys.path.append(parent_dir_name + "/../../../")
from modules.algorithms.cyphers.morse import *
I have also init files in all directories.
I am trying to run tests like:
> python -m unittest discover ./tests
It is customary to use relative imports
from ....modules.algorithms.ciphers.morse import *
This ensures that you import the right Morse file, otherwise you risk importing some module named Morse that you've installed.
Here's two examples to illustrate relative imports, say you have a file named simulation.py that has the line
from .animals import Herbivore
In it. This will import the Herbivore class from a file in the same directory as the simulation.py file
Now imagine that you have a folder named tests, in this folder you have a file named test_animals.py, which has the line
from ..animals import Herbivore
This will import the Herbivore class from a file named animals.py located in the folder above the tests folder.
Finally, note that (at least for Python 3.6) can't run a file that uses relative imports, only import it.
If you mark the directories as modules with __init__.py, you should just be able to import modules via
from modules.algorithms.cyphers import morse
I am trying to import a module from a python file that is in a sibling folder. I read several similar questions here and tried to apply solutions listed there, but I wasn't able to solve the problem.
The structure is as follows:
parentfolder/gfolder/codefolder/fileA.py
parentfolder/gfolder/utilfolder/util.py
gfolder, codefolder and utilfolder all have an __init__.py.
I'm trying to do in fileA.py:
import gfolder.utilfolder.util as util
I also tried adding before the import statement:
sys.path.append(".../parentfolder/")
And that didn't work either:
import gfolder.utilfolder.util as util
ModuleNotFoundError: No module named 'gfolder'
The solution in a similar question says to include __init.py__ in the directories, which I already have.
EDIT:
Now both sys.append and sys.insert work and the problem was that I included a slash at the end of the path. When I took it out, everything worked.
First of all, let me describe you the differences between a Python module & a Python package so that both of us are on the same page. โ
A module is a single .py file (or files) that are imported under one import and used. โ
import aModuleName
# Here 'aModuleName' is just a regular .py file.
Whereas, a package is a collection of modules in directories that give a package hierarchy. A package contains a distinct __init__.py file. โ
from aPackageName import aModuleName
# Here 'aPackageName` is a folder with a `__init__.py` file
# and 'aModuleName', which is just a regular .py file.
Therefore, when we have a project directory named proj-dir of the following structure โคต
proj-dir
--|--__init__.py
--package1
--|--__init__.py
--|--module1.py
--package2
--|--__init__.py
--|--module2.py
๐ Notice that I've also added an empty __init__.py into the proj-dir itself which makes it a package too.
๐ Now, if you want to import any python object from module2 of package2 into module1 of package1, then the import statement in the file module1.py would be
from proj-dir.package2.module2 import object2
# if you were to import the entire module2 then,
from proj-dir.package2 import module2
I hope this simple explanation clarifies your doubts on Python imports' mechanism. ๐
As Andrew Cox answerd int the following thread Import a module from a relative path
You can add the subdirectory to your Python path so that it imports as a normal script
import sys
sys.path.insert(0, <path to gfolder>)
import gfolder
you can also add the directory to the PATH var of the Linux system (I use it while I'm working on a project, at the end i modified the PATH to it's origin value)
if you maintain the following structre than it is working out side the box
parentfolder/gfolder/codefolder/fileA.py
parentfolder/gfolder/utilfolder/util.py
parentfolder/gfolder/main.py
run main.py