Say I have a directory structure like this
$ tree
.
├── A
│ ├── __init__.py
│ ├── conftest.py
│ ├── test_set_1.py
│ └── test_set_2.py
├── B
│ ├── __init__.py
│ ├── conftest.py
│ ├── test_set_1.py
│ └── test_set_2.py
├── C
│ ├── __init__.py
│ ├── conftest.py
│ ├── test_set_1.py
│ └── test_set_2.py
├── conftest.py
├── pytest.ini
I want to define a pytest fixture that I can specify to run once for a particular top-level module (A, B, or C), but not once for every submodule (test_set_1.py, test_set_2.py, etc.)
How can I do this? Can I inject these dependencies inside the __init__.py files somehow?
After a few hours I figured out a working answer: the scope="session" argument for a fixture applies to all submodules. So if A/conftest.py has a session-level fixture in it, it's only run once for all the tests in that submodule, but not run for tests in other submodules.
Related
I am playing around with Sphinx 4.4.0 in a test project. I use sphinx.ext.autodoc and sphinx-apidoc.
But not all py files are recognized. It seams like that files with a starting _ in filename are ignored (e.g. _mypackage.py). But i am not sure if that character is the cause or something else.
Are you aware of an option to modify that behavior?
This is the project structure
sphinx_versuch
├── docs
│ ├── make.bat
│ ├── Makefile
│ └── source
│ ├── conf.py
│ ├── index.rst
│ ├── modules.rst
│ ├── mypackage.rst
│ ├── _static
│ └── _templates
├── LICENSE
├── README.md
├── src
│ ├── mypackage
│ │ ├── a.py
│ │ ├── b.py
│ │ ├── __init__.py
│ │ └── _mypackage.py
│ ├── setup.cfg
│ └── setup.py
└── tests
├── __init__.py
├── test_a.py
├── test_b.py
└── test_mypackage.py
The option --private or -P for sphinx-apidoc make sure that private files (which start with an underscore by convention) are included also.
See also
https://www.sphinx-doc.org/en/master/man/sphinx-apidoc.html#cmdoption-sphinx-apidoc-P
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
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.