python submodule can't find import - python

So I have a module called gann which itself has a number of modules, the problem is that if I run gannController.py directly it fails.
I have the following file structure(some files omitted):
----convert
__init__.py
convert.py
----AI
__init__.py
----gann
gannController.py
----model
__init__.py
modelController.py
----util
now I want to use the gannController.py in the convert.py file.
from AI.gann import gannController
in convert.py does import gannController, however it crashes on the first line which is
from model import modelController
which does work if I run gannController.py directly. As it gives the error:
ModuleNotFoundError: No module named model
So I guess that it's because a submodule uses a submodule of it's own that I cannot run this. Anybody know how to fix this? It's worth noting that I would prefer not to pollute my convert namespace with all of the stuff in model and that I have omitted a few other modules that have the same situation (inside util)

Change your import to be relative:
from .model import modelController

Related

Python ModuleNotFoundError when importing custom modules

I have the following project structure
Project Structure
In utils.py I am importing a parse function form parser.py file like this
from logica.parser.parser import parse
In builder.py I am importing a function from utils.py that uses the parser function like this
from utils.utils import utils
When trying to run all this stuff I get a ModuleNotFoundError:
ModuleNotFoundError: No module named 'logica'
Do you guys have any ideas on how to resolve the issue?
Thanks
In Python you can only import modules installed via pip or files stored in the same directory of the current file.
To solve this problem you can do this:
import sys
sys.path.append(<path-of-the-module>)
I would suggest to read also this.

How to orchestrate your own module in python

The following picture shows a small orchestration for learning how to set up the orchestration when building a module in python
The module1 contains two functions and one depends on the other
outside of the module1 folder there is a main.py file that should import all the functions of the files up.py e double.py
as it can be seen in the screenshot the importing does not work in main.py
This are errors I get when importing:
from module1 import double
double.doublefunc('erer')
ERROR:
AttributeError: module 'module1.double' has no attribute 'doublefunc'
What would be the difference between an empty init.py and importing inside init.py? I had understood that init.py is normally empty, but it looks like there is not always the case.
includes are relative.
so in the library files you have to refer to local files in the same folder. In your case in double.py you have to refer to up.py by from .up import upfunc as mm just like you do in the init.py
in the main.ipynb you have to import the python objects, not the files. so from module1 import doublefunc as dd, etc...

Importing From Another Script in Python

My directory is as such:
isds:
__init__.py
jobs:
__init__.py
adhoc:
__init__.py
test.py
test2.py
My two files look like this.
test.py:
import sys
x = 10
test2.py:
import sys
from isds.jobs.adhoc.test import *
print(x)
When I run "python3 test2.py" from the same directory as test2.py, I get this error: ModuleNotFoundError: No module named 'isds.jobs.adhoc.test'
Why is this happening? I have the init.py files and I think I have the absolute import statement correct... but maybe not?
Thanks!
Since you are importing the module from same directory you can simply import using.
from test import *
in order to import as a package then you need to run the file as a package, so you would navigate to the folder containing isds` and run:
python -m isds.jobs.adhoc.test2
This runs the file as a module instead of a script, and since it gets indexed at the same level that it uses its own imports then the import mechanic you are using works as intended.
If you want to support either running as a script or running as a module you would need something like this:
try:
from isds.jobs.adhoc.test import *
except ModuleNotFoundError:
from test import *
But this can lead to other issues like if a different module not found error occurs and then import test ends up importing something else entirely you can get confusing and misleading error messages, so I'd generally recommend just running all your stuff with the -m flag if you are writing packages.
Also note this method works without any __init__.py files in python 3.7,4. the requirement to add empty init files was removed a while ago I believe.

Python __init__ file can't import modules

I know this question has been asked before but I couldn't get to an answer.
My package folder looks like this, no sub folders, just a flat package folder with .py files in it.
+Package
∣
∣--__init__.py
∣--moduleA.py
∣--moduleB.py
If I run my test.py script from inside the package folder, the imports, classes and methods work fine:
import moduleA.py
import moduleB.py
# ...stuff
Now, if I try to run my package from outside importing import Package, outside being \site-packages I get
File "defaultPathTo\Python\Python38\lib\site-packages\Package\__init__.py", line 1, in <module>
import moduleA
ModuleNotFoundError: No module named 'moduleA'
This is my init file
import moduleA
import moduleB
I tried changing the content to from moduleA import *, from . import moduleA
from .moduleA import (whatever class) seems to work, but I don't want to change all my classes from moduleA.ClassA because it clashes with class names from the other modules.
I think I summed up all the information neede. Thanks for the help
Using from . import moduleA for all imports from within my package did the trick.
I may have done something wrong the first time I tried because I got no known parent folder

Python Module System - Import Sibling Subpackage

In Python, I want a file in a subpackage to import a sibling subpackage. Like so:
/proj
__init__.py
runner_main.py
/subpackageA
__init__.py
helper.py
/subpackageB
__init__.py
runnerB.py
In runner_main.py, I can call import subpackageA just fine.
However, calling from . import subpackageA fails with error
ImportError: cannot import name 'preprocessing' from '__main__'
This isn't a problem, except I want to import helper.py from runnerB. Calling from .. import subpackageA fails with a similar error.
I don't want to put /proj on my system or Python path; I want it to work as a standalone package. How can I make this simple case work?
Try
from subpackageA import classA, functionB, constantC
It depends on where you run your code. You can import helper.py from runnerB. But If you run runnerB directly, it will gives you same error as you described. However If you run from any py file placed in the parent directory that import runnder module, it will work.

Categories

Resources