My current project structure is
package
|
- __init__.py
|
- minipackage1
|
- __init__.py
- file1.py
- file2.py
- minipackage2
|
- __init__.py
- file3.py
- file4.py
extrenal init looks like
__all__ = ["minipackage1", "minipackage2"]
and others like:
__all__ = ["file1", "file2"]
and
__all__ = ["file3", "file4]
But on running my script it says:
AttributeError: module 'package' has no attribute 'minipackage1'
So, where am I wrong and how to fix this?
Related
I have the current directory structure:
- api
|
- app.py
|
- tests
|
- test_app.py
What is the correct way to import app.py into test_app.py?
so I have a module/directory called A and it has init.py file and in it, it has another module/directory called B which have its init.py and a file called function.py which has a function called dummy()
here is the structure of directories
A
|-- __init__.py
|
|-- B
|
|-- __init__.py
|-- function.py
so what I want is to be on the same directory that contains directory A and do that
from A import *
dummy()
what I have done is do that in B/init.py
from dummy import *
and that in A/init.py
import B
and I can do that
from A.B import *
I want to write A instead of A.B
I changed your import code a bit and it seems to work now like you wanted.
So in the B directory's init.py it has:
# __init__.py in B
from .function import *
In the A directory's init.py:
# __init__.py in A
from .B import *
Now, when I run Python shell in the directory that contains A and and use from A import *, it calls dummy() with no problem.
However, there are discussions on using wildcard imports in Python. Check this post for example: Should wildcard import be avoided?
So I am trying to access to a file contained inside a folder which himself is inside the parent folder :
_PARENT
- __init__.py
- Parser
- __init__.py
- myPars.py
- Getter
- __init__.py
- getSomething.py
- Setter
- __init__.py
- setSomething.py
What I need here is to access to 'myPars' from the 'getSomething'.
I tried this :
from ..Parser import myPars
But I get :
ImportError: attempted relative import with no known parent package
Any help would be appreciated,
Thanks.
I just found the answer, all I needed was to add the folder to the path like :
pth = os.path.abspath(os.getcwd())
sys.path.insert(0, pth)
****solved: added __init__.py to Test/ and renamed testcode.py to test_code.py. To run tests cd -> Zee and type pytest****
Structure:
|--Zee/
| |--Test/
| | |--__init__.py
| | |--test_code.py
| |--Codetotest/
| | |--code.py
in code.py
class Foo():
some code...
in testcode.py
from Codetotest.code import Foo
def test_foo():
assert ...
When I move to the Zee directory in my command line and run pytest Test/testcode.py I get ModuleNotFoundError: No module named Zee. How can I fix this?
I tried making Test a module by adding Test/__init__.py as suggested here. Ran from multiple directories, no dice.
Pytest version 5.3.4, imported from python 3.6
What I don't understand is, when I add __init__.py to Zee/, it gives me the same error
You need a __init__.py in the module directory.
Here's a typical project structure:
|--zee-project-directory/
| |--tests/
| | |--test_zee.py
| |--zee/
| | |--__init__.py
| | |--code.py
code.py
class Foo():
some code...
test_zee.py
from zee.code import Foo
def test_foo():
assert ...
Somehow I couldn't find the exact answer to this elsewhere on SO.
Given:
root\
__init__.py
main.py
folder0\
__init__.py
folder1\
__init__.py
class1.py
folder2\
__init__.py
class2.py
Is there a way to import the top level directory as a whole? e.g.
# main.py
import folder0
obj1 = folder0.folder1.class1.Class1()
obj2 = folder0.folder2.class2.Class2()
Or do I have to import the modules directly? e.g.
# main.py
from folder0.folder1 import class1
from folder0.folder2 import class2
obj1 = class1.Class1()
obj2 = class2.Class2()
Sure. You just need to add the relevant imports into the __init__.py all the way down. e.g.:
# folder2/__init__.py
from . import class2
and
# folder0/__init__.py
from . import folder1
from . import folder2
and so-on.