Python Module System - Import Sibling Subpackage - python

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.

Related

Handle import of modules inside of a project

I have the following project architecture (simplified):
root/
main.py
mypackage/
__init__.py
module1.py
module2.py
ci_scripts/
script1.py
script2.py
doc/
doc
where the root is nothing more than a folder.
How do I import module1 and module2 in script1.py for example?
I tried to:
Add the relative path inside script1.py: from ..mypackage import module1
The absolute path
Use of sys package to append the path of mypackage
To give more context, I was expecting that the following code inside of script1.py would work:
from ..mypackage import module1
module1.func1()
but I get:
ImportError: attempted relative import with no known parent package
And when I try to use the absolute path:
import mypackage.module1
module1.func1()
I get the following error:
ModuleNotFoundError: No module named 'mypackage'
Your absolute import probably does not work because your root folder is not set to be mypackage. You can see here on how to do that: python: Change the scripts working directory to the script's own directory
Alternatively, you can use relative imports. You are correctly importing with from ..mypackage import module1 - however, you cannot execute a script directly with a relative import. You need to import the script into some other module which you are then executing. See explanation here: Relative imports in Python 3
I've had similar problems in the past and thus I've created a new import library: ultraimport
It gives you more control over your imports and lets you do file system based relative and absolute imports that do always work.
In your script1.py you could then write:
import ultraimport
module1 = ultraimport("__dir__/../mypackage/module1.py")
This will always work, no matter how you run your program and independent of all external factors.

python submodule can't find import

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

Importing from parent directory python

I have the following structure:
app/
code/
script.py -- has a function called func
main.py
How can I import script.py from main.py ?
I tried from code.script import func and I got ModuleNotFoundError: No module named 'code.script'; 'code' is not a package
Place a __init__.py file in the code directory. This will allow your main.py code to import it as a module like you have tried there.
Indeed the best way is to add __init__.py in code directory because when a regular package is imported __init__.py file is implicitly executed and the objects it defines are bound to names in the package’s namespace.
FYI, as an alternative you can also to this in your main.py before the import:
import sys
sys.path.append("/path/to/script.py")

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

Best way to import a package located in a sibling directory

My file stucture is :
top\
my_package\
__init__.py
functions.py
scripts\
test.py
main.py
I would like to import the content of functions.py in test.py.
In main.py, I can import functions.py with from my_package.functions import ....
I was expecting to be able to import functions.py in test.py with something like from ..my_package.functions import ... but it raises the following error :
SystemError: Parent module '' not loaded, cannot perform relative import
top directory shoudln't be a package because I want to be able to run main.py without being running a script in a package.
What is the proper/pythonic way import functions.py from test.py ?
I could add my_package to the PYTHONPATH, but my code would be less portable. I'm using Python 3.5
This is happening because as far as python is concerned my_package and scripts directories are not related to each other.
To solve your problem you can add __init__.py to your top directory and try using
from ..function import *

Categories

Resources