How to orchestrate your own module in python - 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...

Related

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

'ModuleNotFoundError' when trying to import script from imported script

My folder structure:
ttsTacotron.py
Tacotron-2
|..
|tacotron|
|train.py
|synthetizer.py
|...
|synthesize.py # imports hparams.py
|hparams.py
...
When I call synthesize.py directly it works fine, all of its imports get processed successfully. When I import synthesize in the ttsTacotron.py and call it, it fails on importing synthesize's modules. Specifically, it fails on importing hparams.
ttsTacotron.py:
import fire
import sys
import os
import importlib
foobar = importlib.import_module("Tacotron-2.synthesize")
Tacotron folder in question is this repository but the issue is unlikely to be specific to it.
Remarks: I use importlib to handle having - in the subfolder. Can't really rename it for various reasons.
My goal: Be able to call synthetize's methods & be able to import tacotron modules from a script that is in the root folder.
This is because, when running ttsTacotron.py, Python looks up all non-relative imported modules in the directory containing ttsTacotron.py (and in the system module directories, which isn't relevant here), yet hparams.py is in the Tacotron-2 directory. The simplest fix is probably to add Tacotron-2 to the list of directories in which modules are looked up; this also eliminates the need to use importlib.
import sys
sys.path.insert(1, 'Tacotron-2')
import synthesize as foobar

No module named "file1.py"; test1 is not a package

I'm trying to modularize my code and I'm having issues with it.
My folder is constructed like this:
code
|_main.py
|_test1
|_calcA.py (which contains a method)
|_test2
|_calcB.py (which contains another method)
|_test3
|_calcC.py (which contains another method)
Now my main.py contains these lines:
import sys; import pprint
pprint.pprint(sys.path)
from test1.calccircle.py import calcA
from test2.calctriangle.py import calcB
from test3.calccarre.py import calcB
The following error comes:
ImportError: No module named 'test1.calcA.py'; 'test1.calcA' is not a package
You don't need to specify .py while importing modules. Python knows your modules are Python code only. So remove .py while importing modules in Python.
Test1 is a folder or directory and you are trying to access it like a package.
If you want to access it that way you have to insert init.py file in your folder. And also you don't need to specify .py when importing!
add __init__.py inside your directories
You can do like this
from test1.calcA import calcA where calcA can be a method.

Entire module is not detected by __init__.py

I have a relatively small python program which is setup like this
Root
--Network
--DTO
Lots of py files which contain classes.
Other py files in the project
Both in the Network and the DTO folder there is an empty __init__.py.
When I do from Network import DTO, import Network, Import Network.DTO I cannot use the module at all. The debugger says it's completely empty. The root contains the py file I am actually executing.
Both the the __init__.py files in the Network and DTO folder are compiled to pyc while all actual python files aren't.
Does anyone have any idea what I am doing wrong? I am using python 2.7
For that, you need to import the submodules in your __init__.py. It becomes more difficult to add an import for every submodule if you have many, as you do. In the case of many submodules (and in general), use __all__.
Here's an example for Root/Network/DTO/__init__.py:
__all__ = [
'sample_module',
...
]
for module in __all__:
__import__('DTO.%s' % module)
Now you can do from Network.DTO import sample_module. The same idea holds for your other modules, as well.

"No module named MyClass" error on PyCharm

I am using PyCharm Community Edition 2016.3.1 but when I have two+ python files on the same directory, if I import one of them into the other it gets underlined saying:
# main.py
import MyClass1
No module named MyClass1 less... (Ctrl+F1) This inspection detects names that should resolve but don't. Due to dynamic dispatch and duck typing, this is possible in a limited but useful number of cases. Top-level items are supported better than instance items.
but when I execute my main.py it works properly.
Other regular imports like import sys don't get underlined.
If those two python files are under the same directory eg. MyDirectory, you need to import the classes using the MyDirectory as the root. So for example if you have the below project structure:
└── MyDirectory
├── file1.py (MyClass1)
└── file2.py (MyClass2)
To import the MyClass1 into the file2.py you can do it as below:
from MyDirectory.file1 import MyClass1
The reason you are getting this error is because you are not importing correctly.
Python imports follow this syntax.
import filename
This means you need to have a file name filename.py in the current directory. You can also import a specific class from that file like so.
from filename import MyCalss

Categories

Resources