I have the following package structure:
.
├── README.md
├── common
│ ├── __init__.py
│ ├── analysis
│ │ ├── __init__.py
│ │ └── base_analysis.py
│ ├── logger
│ ├── __init__.py
│ └── logger.py
└── scripts
└── test_analysis
└── run.py
I would like to access logger in base_analysis.py. If I do this:
from ..logger import Logger
I am getting this error:
ValueError: attempted relative import beyond top-level package
How to import a sub-package from the parent package?
Note: I am running the script from scripts/test_analysis using:
python run.py
following changes to the calling python run.py script fixed it;
from logger.logger import Logger
Related
Pycharm imports works correctly, but when I execute test_settings.py in the terminal, I have this error:
ImportError while importing test module '/home/ed/PycharmProjects/TTime/tests/test_settings.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
tests/test_settings.py:3: in
from ttime import app, db
E ModuleNotFoundError: No module named 'ttime'
Here is my Flask project structure. I tried to include init.py in the main folder and in the tests folder, but nothing changes. It seems like somethings is wrong with python paths.
.
├── config.py
├── README.md
├── requirements.txt
├── runserver.py
├── tests
│ ├── functional
│ │ └── __init__.py
│ ├── pytest.ini
│ ├── test_settings.py
│ └── unit
│ ├── __init__.py
│ └── test_models.py
└── ttime
├── __init__.py
├── models.py
├── routes.py
├── static
└── templates
├── base.html
└── index.html
I've got a directory structure like:
Folder_in_PYTHONPATH/
├── Package1
│ ├── __init__.py
│ ├── src
│ │ ├── Class1.py
│ │ ├── Class2.py
│ │ └── __init__.py
│ └── test
│ └── testfile.py
├── Package2
│ ├── __init__.py
│ ├── src
│ │ ├── Class1.py
│ │ ├── Class2.py
│ │ └── __init__.py
│ └── test
│ ├── test1.py
│ └── test2.py
.
.
.
When I import things from this folder, I need to always type
import Package1.src.Class1
Is there any way to set up my __init__.py so that I can just type
import Package1.Class1
instead?
Add them into your packages' __init__.py files so they look like:
from src import Class1
from src import Class2
Have a look at the docs
I would recommend putting the *.py files in the top level folder of their package to get the import Package_1.Class1 behaviour you are after. The unit tests can stay in their own folder to keep them separate.
I have a my projects set up like so(the name package is used isn't the name of the directory only for this example it has been used):
.
├── Installer\ Script.iss
├── LICENSE
├── README.md
├── TODO.md
├── docs
├── requirements.txt
├── resources
│ ├── logo.hqx
│ ├── logo.icns
│ ├── logo.ico
│ └── qt.conf
├── setup.py
└── project
├── Main.py
├── Controller.py
├── Updator.py
├── Updator.pyc
├── __init__.py
├── tests
│ ├── TestController.py
│ ├── TestUpdator.py
│ ├── TestUpdator.pyc
│ └── __init__.py
└── ui.py
In my Main.py how should I import ui.py? Currently my code for importing looks like:
import project.ui as ui
Is this correct? As when I freeze the project with py2app it doesn't like my imports.
If the the package that you want to import in the same directory, you just write "import ui" in your main.py can work as you want it to.If you want to import ui in you setup.py(just for example), you can do it what you ask above.
Hope this can help you .
When I run an import in my development environment, I seem to get the wrong module.
How do I make sense of this?
$ python -c "import breathe.renderer.rst.doxygen.compound as com; print com"
<module 'breathe.parser.doxygen.compound' from 'breathe/parser/doxygen/compound.pyc'>
The layout of the breathe directory is:
breathe
├── directives.py
├── finder
│ ├── doxygen
│ │ ├── base.py
│ │ ├── compound.py
│ │ ├── index.py
│ │ └── __init__.py
│ └── __init__.py
├── __init__.py
├── nodes.py
├── parser
│ ├── doxygen
│ │ ├── compound.py
│ │ ├── compoundsuper.py
│ │ ├── index.py
│ │ ├── indexsuper.py
│ │ ├── __init__.py
│ ├── __init__.py
├── process.py
├── renderer
│ ├── __init__.py
│ └── rst
│ ├── doxygen
│ │ ├── base.py
│ │ ├── compound.py
│ │ ├── domain.py
│ │ ├── filter.py
│ │ ├── index.py
│ │ ├── __init__.py
│ │ └── target.py
│ ├── __init__.py
└── transforms.py
Check your breathe/renderer/rst/__init__.py file. I suspect that you have something like
from breathe.parser import doxygen
What happens is that when a module is imported, the module's __init__.py is executed. Then the next submodule is imported and it's __init__.py is run.
So, in the above situation, you'll get breathe.renderer.rst importing fine, but when running
breathe.renderer.rst.__init__, the breathe.parser.doxygen module is bound to the local name doxygen overriding the doxygen submodule. So you should see something like this:
>>> from breathe.renderer import rst
>>> print(rst.doxygen)
<module 'breathe.parser.doxygen' from 'breathe/parser/doxygen/__init__.pyc'>
Possible solutions:
Use absolute imports in __init__.py
Keep as much code as possible out of __init__.py
Use import inside the functions it's needed rather than globally. (This can also resolve circular import problems)
Note that there is nothing that prohibits code in __init__.py, and several modules in the stdlib do it too. However, because of exactly this sort of issue it is generally considered a good idea to avoid doing it too much.
New-ish to python, but I though I had everything set up right. When I run python -m unittest test.unit.test_oyez_case, I get AttributeError: 'module' object has no attribute 'test_oyez_case'
Sorry this is a frequent question, none of the responses were helpful to me
Here's my file structure:
├── README.md
└── test
├── __init__.py
├── __init__.pyc
├── integration
│ └── __init__.py
└── unit
├── __init__.py
├── __init__.pyc
├── mocks
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── responses.py
│ └── responses.pyc
├── test_oyez_case.py
└── test_oyez_case.pyc
Here's test/unit/test_oyez_case.py:
import json
import unittest
import responses
from mocks import responses as api_responses
from puppy_scotus.oyez_case import OyezCase
class TestOyezCase(unittest.TestCase):
. . .
if __name__ == '__main__':
unittest.main()
Try to run it using a commandline like python test_oyez_case.py. It could be that one of your other imports is incorrect. ;-)