The directory structure:
[app]
start.py
import package1
[package1]
__init__.py
print('Init package1')
import module1
import subpackage1
module1.py
print('package1.module1')
import package1 # this works OK
[subpackage1]
__init__.py
print('Init package1.subpackage1')
import module1
module1.py
print('Init package1.subpackage1.module1')
#from package1 import subpackage1 # ImportError: cannot import name subpackage1
#from .. import subpackage1 # ImportError: cannot import name subpackage1
#import . as subpackage1 # SyntaxError: invalid syntax
import package1.subpackage1 as subpackage1 # AttributeError: 'module' object has no attribute 'subpackage1'
To avoid problems caused by circular imports in subpackage1.module1 i want to import module subpackage1 in order to refer to other modules from subpackage1 in form subpackage.module2. Because if i do from . import module2 the reference to module2 could not yet exist in subpackage1 when i try to this import.
I have tried 4 different approaches - none of them worked - see the comments in the code.
Any help?
Some time ago subpackage1 was top level package and it worked (see how this works in the source of package1.module1. Now, when i moved it one level down - i have this problem... I know that i can add package1 dir to sys.path, but that's ugly.
I used this hack, which worked for me:
#import package1.subpackage1 as subpackage1 # AttributeError: 'module' object has no attribute 'subpackage1'
subpackage1 = sys.modules[__name__.rpartition('.')[0]] # parent module
Or you can try this:
from package1 import subpackage1
which works in some cases: https://stackoverflow.com/a/24968941/248296
I'm not not exactly sure what you are trying to do, but your example might be a lot easier to understand if you used absolute imports and avoided putting code in __init__ modules.
Try something like this:
[app]
start.py
print('Start')
from package1 import module1
[package1]
__init__.py
print('Init: package1')
module1.py
print('Load: package1.module1')
from package1.subpackage1 import module1
[subpackage1]
__init__.py
print('Init: package1.subpackage1')
module1.py
print('Load: package1.subpackage1.module1')
from package1 import subpackage1
After running start.py, you should get output like this:
Start
Init: package1
Load: package1.module1
Init: package1.subpackage1
Load: package1.subpackage1.module1
Related
Im having trouble importing from sibling directories.
I would like to set up my package so that all of these aspects simultaneously work.
Here is my module setup:
package/
__init__.py
code/
__init__.py
model.py
helper.py
notebooks/
__init__.py
notebook1.ipynb
Imports in model.py:
from helper import *
When running from the command line in package/ I set PYTHONPATH=. and I can run model.py
Imports in notebook1.ipynb:
First scenario:
sys.path.insert(0, os.path.abspath('..'))
from code.model import MetaModel
Results in: ModuleNotFoundError: No module named 'code.model'; 'code' is not a package
Second scenario:
sys.path.insert(0, os.path.abspath(os.path.join('../code')))
from model import MetaModel
This works. Why can I not use the first scenario.
I have a package:
mypkg
|-- __init__.py
|-- __main__.py
|-- mod1.py
__main__.py
from mypkg import mod1
def main():
print("Hello from mypkg main!")
print(mod1.mod1_msg())
if __name__ == '__main__':
main()
File __init__.py is empty.
I need to run mypkg from specified path for example:
python -m mypath.mypkg
When I run the mypkg in this way then I get the error:
ImportError: cannot import name 'mod1' from 'mypkg' (unknown location)
This could be solved when I change from mypkg import mod1 to from . import mod1 but PEP8 recommends absolute imports over relative imports.
Another solution that I found is to add:
import sys
from os import path
sys.path.append(path.dirname(path.dirname(path.abspath(__file__))))
to __init__.py file
Then everything works great.
So the question is: What is the better solution? Using relative imports or manipulate sys.path to get the package run properly or may be exists another solution of this problem?
My project structure is as follows:
proj/
src/
__init__.py
etc/
__init__.py
visitor.py
obj/
__init__.py
node.py
tests/
__init__.py
visitor_tests.py
I'm having issues importing visitor.py in my visitor_tests.py class because there is an import for node.py in it which can not be found.
In visitor_tests.py i'm importing the visitor itself using:
from src.etc.visitor import Visitor
But I get the following error:
ModuleNotFoundError: No module named 'obj.node'; 'obj' is not a package.
In visitor.py I have the following import for node:
from obj.node import Node
I run the tests using pytest tests/visitor_tests.py from the proj/ root.
instead of using src.etc.visitor,obj.node user sys.path.append in your visitor_tests.py & visitor.py
import sys
sys.path.append("/path/to/obj")
sys.path.append("/path/to/etc")
My directory structure is:
[File1]
[Package1]
__init__.py
from Package1 import module1
from Package1 import module2
...
module1.py
module2.py
...
I want to import a package so that using a class like
from File1 import Package1
Package1.Module1.Class1()…
is possible.
When I try
from File1 import Package1
I always get the error:
cannot import name Package1
I think that Circular imports may be the problem, but I don't know how to fix it.
1) You need to add __init__() in File1 folder also (empty also ok).
2) Change __init__() inside Package1 as follows:
__init__.py
from File1.Package1 import module1
from File1.Package1 import module2
Then from your python file you can access like
from File1 import Package1
x=Package1.module1()
x=Package1.module2()
I think this will work....
have fun
I have a python project, I divided it into two parts, a library part and a
binary part.
For organization reasons, I wanted to have a hierarchy like this:
project/
lib/
__init__.py
module1.py
module2.py
bin/
binary1.py # contains: import module1
binary2.py # contains: import module2
doc/
...
The problem is, of course, the simple import in binary{1,2}.py doesn't work, at
least in the coding phase (before installation).
Any suggestions? How do you test your code when you're coding?
I use absolute imports everywhere.
project/
__init__.py
lib/
__init__.py
module1.py
module2.py
bin/
__init__.py
binary1.py # contains: import module1
binary2.py # contains: import module2
doc/
...
Setting PYTHONPATH to the directory above project, you can import module1 from binary1.py like this:
from project.lib import module1
The __init__.py files can make importing simpler, let's say in each file you have a class. Usually you would have to import the class like this (inclduding filename):
from project.lib.module1 import Module1
But if you edit lib/__init__.py to contain this line:
from project.lib.module1 import Module1
You can use the namespace of lib to import Module1 directly from lib:
from project.lib import Module1