Importing module item of subpackage from another subpackage - python

I have this project structure:
root_package/
root_package/packA/
root_package/packA/__init__.py (empty)
root_package/packA/moduleA.py
root_package/packB/__init__.py (empty)
root_package/packB/moduleB.py
root_package/rootModule.py
In the rootModule.py I have from packA.moduleA import ModuleAClass.
At the packA.moduleA.py I have this from root_package.packB.moduleB import ModuleBItem.
When running rootModule either via PyCharm or the terminal with python ./rootModule.py I am getting this error:
Was this the right way of importing?
Traceback (most recent call last):
File "/project_dir/rootPackage/rootModule.py", line 7, in <module>
from packA.moduleA import ModuleAClass
File "/project_dir/rootPackage/packA/moduleA.py", line 8, in <module>
from rootPackage.packB.moduleB import module_b_method
File "/project_dir/rootPackage/rootModule.py", line 7, in <module>
from packA.wavelet_compression import WaveletCompression
ImportError: cannot import name WaveletCompression
How to solve this?
Update 1
I've added a test file at the project_folder (not the root_package folder).
So the current directory structure is this:
project_folder/
project_folder/root_package/
project_folder/root_package/packA/
project_folder/root_package/packA/__init__.py (empty)
project_folder/root_package/packA/moduleA.py
project_folder/root_package/packB/__init__.py (empty)
project_folder/root_package/packB/moduleB.py
project_folder/root_package/rootModule.py
project_folder/test_rootModule.py
I haven't made the project_folder a package (no __init__.py file) since, the test_rootModule is simply a script to help me run the experiments.
So, in root_package/packA/moduleA.py, after changing the from root_package.packB.moduleB import ModuleBitem, to from packB.moduleB import ModuleBitem, as the answer suggests, it works.
But now there are two problems:
1. PyCharm doesn't agree with the change:
I cannot run my experiments from the project_folder/test_rootModule.py script.
I got this error:
Traceback (most recent call last):
File "project_folder/test_rootModule.py", line 8, in
from root_package.rootModule import rootModuleClass
File "project_folder/root_package/rootModule.py", line 7, in
from packA.moduleA import ModuleAClass
File "project_folder/root_package/packA/moduleA.py", line 8, in
from packB.moduleB import module_b_item
ImportError: No module named packB.moduleB
I cannot seem to get the 2nd Traceback to look like a code segment.
Update 2
What solved the problem was going to the Project: project_name > Project Structure dialog in PyCharm, selecting the root_package and then setting it as a Sources folder.
Now, I can run via the IDE both the rootModule and the test_rootModule.
Although, I cannot get to run the test_rootModule from the terminal.
The test_rootModule has these imports:
from root_package.rootModule import RootModuleClass
from root_package.packB.moduleB import module_b_item
I am at the project_folder dir, and run python ./test_rootModule.py and get this error:
Traceback (most recent call last):
File "./test_rootModule.py", line 8, in <module>
from root_package.rootModule import RootModuleClass
File "project_folder/root_package/rootModule.py", line 7, in <module>
from packA.moduleA import ModuleAClass
File "project_folder/root_package/packA/moduleA.py", line 8, in <module>
from packB.moduleB import module_b_item
ImportError: No module named packB.moduleB

If you are running all your code from within this path:
project_folder
Then you should ensure that all your modules that reside in root_package are referenced by that first. So for example:
from root_package.modA import foo

Related

AttributeError: module 'datetime' has no attribute 'date' on import yaml

one of my code lines is
import yaml
which was installed on python 3.7 using pip install pyyaml
the following error arises
Traceback (most recent call last):
File
"C:/code/EPMD/Kodex/Applications/EPMD-Software/Sandbox/peer_changing_send_rate.py",
line 1, in
from TestPeer.TestPeerChangingSendRate import TestPeerChangingSendSpeed
File "C:\code\EPMD\Kodex\Applications\EPMD-Software\TestPeer\TestPeerChangingSendRate.py",
line 1, in
from .TestPeer import TestPeer
File "C:\code\EPMD\Kodex\Applications\EPMD-Software\TestPeer\TestPeer.py",
line 4, in
from BaseProcess.ZmqPeerClass import ZmqPeer
File "C:\code\EPMD\Kodex\Applications\EPMD-Software\BaseProcess\ZmqPeerClass.py",
line 2, in
from .ZmqPublisherClass import ZmqPublisher
File "C:\code\EPMD\Kodex\Applications\EPMD-Software\BaseProcess\ZmqPublisherClass.py",
line 10, in
from . import ZmqProcessClass
File "C:\code\EPMD\Kodex\Applications\EPMD-Software\BaseProcess\ZmqProcessClass.py",
line 5, in
from .ConfigBaseClass import ConfigBase
File "C:\code\EPMD\Kodex\Applications\EPMD-Software\BaseProcess\ConfigBaseClass.py",
line 3, in
import yaml
File "C:\code\EPMD\Kodex\venv\lib\site-packages\yaml__init__.py", line 9, in
from .dumper import *
File "C:\code\EPMD\Kodex\venv\lib\site-packages\yaml\dumper.py", line 6, in
from .representer import *
File "C:\code\EPMD\Kodex\venv\lib\site-packages\yaml\representer.py", line
263, in
SafeRepresenter.add_representer(datetime.date, AttributeError: module 'datetime' has no attribute 'date'
How to I get import yaml to work?
You probably have a file called datetime.py in one of the directories shown in the error message (most probably C:/code/EPMD/Kodex/Applications/EPMD-Software/Sandbox/), if you do then you need to rename it to something that will not shadow any other Python module.
The reasoning is that it masks the actual datetime module, since files/directories/modules in the current working directory have precedence over the modules installed in the site-packages directory (in which the built-in and installed modules live). If both locations contain an a module then import a will import the local a module instead of the (probably) intended a module from site-packages.
When yaml\representer.py did import datetime it imported your datetime.py file/module which does not have a date attribute, which is why an AttributeError was raised when it later tried to use datetime.date.

ImportError: cannot import name 'scan_videos' from 'subliminal'

I'm using the module subliminal to easily retrieve subtitles from the internet.
The CLI works perfectly, but I cannot import it as a module in a .py file.
The simplest code ever:
import os
from subliminal import scan_videos
videos = scan_videos('./')
print(videos)
And I always have this error:
Traceback (most recent call last):
File "subliminal.py", line 2, in <module>
from subliminal import scan_videos
File "/Users/louisbertin/Desktop/videos/subliminal.py", line 2, in <module>
from subliminal import scan_videos
ImportError: cannot import name 'scan_videos' from 'subliminal'
I don't know what I'm doing wrong. I had copy-pasted the code from the documentation:
https://github.com/Diaoul/subliminal
Seems like you called your script subliminal.py, which makes Python find it instead of the installed module. Rename the script and you should be fine.

Python: import variable from main file at package module

i try to use a complex structure in flask restfull, my structure is the following:
main.py
-models
-- __init__.py
--modelA.py
-resources
--__init__.py
--resourceA.py
I have a variable in main.py and i need this variable in models.modelA and i need also models.modelA in resources.resourceA. At this point everything is ok. When i start my app, i get the following error:
Traceback (most recent call last):
File "main.py", line 12, in <module>
from resources.resourceA import functionA
File "/var/www/project/resources/resourceA.py", line 11, in <module>
from models.modelA import *
File "/var/www/project/models/modelA.py", line 8, in <module>
from main import mainvariable
File "/var/www/project/main.py", line 12, in <module>
from resources.resourceA import functionA
ImportError: cannot import name functionA
I hope your help
What you basically have here is called a circular import. As the traceback states, your main module is importing functionA from resources.resourceA. And resourceA is importing models.modelA which is in turn trying to import main which again requires resources.resourceA.
Unless a particular order is resolved, python interpreter cannot understand how to resolve the modules. You can however solve this problem in an easier way.
If the models.modelA doesn't require a module level import for main, you can shift the import to the function / class scope where you need its imports.
See here for more on circular imports.

Weird relative import resolution error in python 3.2.3

I encoutner the following import resolution error in a program of mine:
I am: homie.translators.is24.rest.translator
Traceback (most recent call last):
File "/usr/local/sbin/is24rest", line 3, in <module>
from homie.interfaces.is24.rest import __main__
File "/usr/local/lib/python3.2/dist-packages/homie/interfaces/is24/rest/__init__.py", line 8, in <module>
from .exporter import Exporter
File "/usr/local/lib/python3.2/dist-packages/homie/interfaces/is24/rest/exporter.py", line 12, in <module>
from homie.translators.is24.rest.translator import Translator
File "/usr/local/lib/python3.2/dist-packages/homie/translators/is24/rest/translator.py", line 9, in <module>
from .factories.rest.restFactory import RestFactory
ImportError: No module named factories.rest.restFactory
The content of /usr/local/lib/python3.2/dist-packages/homie/translators/is24/rest/translator.py is
"""
Created on 18.06.2014
#author: Richard Neumann
"""
print('I am: ' + str(__name__))
from homie.translators.abc import Translator as T
from .factories.rest.restFactory import RestFactory
from .factories.openimmo.openimmoFactory import OpenImmoFactory
class Translator(T):
<snip>
Also, the relative module exists:
root#srv:/usr/src/is24-translator# ls /usr/local/lib/python3.2/dist-packages/homie/translators/is24/rest/factories/rest/
abc.py attachments __init__.py __pycache__ realestates restFactory.py
Why does python3 try to do an absolute import here?
I do only encounter this problem under Debian 7 with python 3.2.3.
Under Arch w/ python 3.4.1 it works just fine.
Am I missing something?
There was the __init__.py missing in .factories, which caused this error.
Took me a while to realize that.

Module cannot make absolute imports to its own branch in the package tree?

If you have a package structure as this:
foldertest/
__init__.py
a/
__init__.py
asub/
__init__.py
b/
__init__.py
foldertest.__ini__.py:
import a
foldertest.a.__init__.py:
import foldertest.a.asub
print foldertest.a.asub
If I from the folder above foldertest/ run the python shell and issue import foldertest I receive the following error:
>>> import foldertest
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "foldertest/__init__.py", line 1, in <module>
import foldertest.a
File "foldertest/a/__init__.py", line 4, in <module>
print foldertest.a.asub
AttributeError: 'module' object has no attribute 'a'
Whereas if I would change foldertest.a.__init__.py to import foldertest.b instead and try printing that I would receive:
>>> import foldertest
<module 'foldertest.b' from 'foldertest/b/__init__.pyc'>
>>>
Am I doing something wrong or is not possible to use absolute referencing to a package's/module's own branch in the package tree? (also PyDev seems to disapprove of writing imports like this) I wanted to have absolute package references for consistency. Also I do not know of any "best practice" for writing import statements or structuring packages that suggest against this.

Categories