Import python files from git submodule without sys.path.append - python

I have a git project ('submodule_folder') cloned as a submodule inside my git project ('my_project') with the following file structure:
my_project
submodule_folder
submodule_folder2
__init__.py
submodule_file.py
file_to_import.py
my_file1.py
my_file2.py
I want to import 'file_to_import.py' from 'my_file1.py' but 'file_to_import.py' imports 'submodule_file.py'.
When doing the following import from 'my_file1.py' I get a ModuleNotFoundError: No module named 'submodule_folder2'.
from submodule_folder import file_to_import
Using Python3, is there any way to do this import without writing sys.path.append('submodule_folder/') in each of my files? I'd prefer not to modify the content of 'submodule_folder'.

If file_to_import.py has
from submodule_folder2 import submodule_file
it worked in Python 2 because in Py2 it was implicit relative import. Python 3 disabled this import mode, it only allows absolute import or explicit relative import. Either
from submodule_folder.submodule_folder2 import submodule_file
or
from .submodule_folder2 import submodule_file

Related

Python - No module named "modules"

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.

Cannot import package from a different folder

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

Importing a class in another folder

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

dot_module and dot_dot_module when import module

I see many people use the following import methods in their projects:
from .module1 import a,b
from ..module2 import *
The module1 and module2 are a .py file but not a folder for package. What's the differences to the import module? Does it mean to import the module in current and ../ folder? But when I try to import another file in same folder, it said:
import .other
>>> SyntaxError: Invalid syntax
from .other import *
>>> ValueError: Attempted relative import in non-package
I'm curious on it. Thanks~
What you see is relative imports. They allow you to import modules by specifying their relative paths, without hard-coding the name of the package in which the modules are defined.
Does it mean to import the module in current and ../ folder?
Yes.
See PEP 328 for more details. Note it says:
Relative imports must always use from <> import; import <> is always
absolute.
which is why you get the SyntaxError when trying import .foo.
The ValueError is probably because you are running the importing file as a script (and it used to confuse me a lot). You need to run it as a package (using the -m switch) for relative imports to work. That is, suppose foo.py relative-imports other modules, you can't run it by
$ python foo.py # non-package error
Instead you do
$ python -m foo
See the related question: How to do relative imports in Python.

Python unittest failing to resolve import statements

I have a file structure that looks like the following
project
src
__init__.py
main.py
module.py
secondary.py
test
test_module.py
module.py
import secondary
x = False
secondary.py
pass
test_module.py
from unittest import TestCase
from src import module
class ModuleTest(TestCase):
def test_module(self):
self.assertTrue(module.x)
Invoking python3 -m unittest discover in /project/ gives an error:
File "/Users/Me/Code/project/test/test_module.py", line 6, in <module>
from src import module
File "/Users/Me/Code/project/src/module.py", line 1, in <module>
import secondary
ImportError: No module named 'secondary'
What can I do so that secondary.py is imported without error?
In Python 3 (and Python 2 with from __future__ import absolute_import), you must be explicit about what module you want when importing another module from the same package. The syntax you're using in module.py (import secondary) only works if secondary is a top-level module in a folder in the Python module search path.
To explicitly request a relative import from your own package, use from . import secondary instead. Or, make an absolute import, using the name of the package as well as the module (from src import secondary, or import src.secondary and use src.secondary elsewhere the module instead of just secondary).
Try adding this to the top of the test file
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
Note: Depending on the project structure we have to specify the root folder

Categories