Pytest location issues - python

I have one problem with pytest. I am creating objects and I like to test these objects if they are complete or buggy. Now the thing: When I put the test script into the src-folder, it works fine. But when I want to use it in a tests folder, I get always an error, that if I like to load my object, a module is not found.
in this structure it works:
my-app/
├─ output/
├─ input/
├─ src/
│ ├─ some_package/
│ ├─ some_code.py
│ ├─ test.py
This structure gives me a ModuleNotFoundError:
my-app/
├─ output/
├─ input/
├─ src/
│ ├─ some_package/
│ ├─ some_code.py
├─ test/
│ ├─ test.py (with changed the relative paths)
I guess that pytest has some problems with using the correct path.

Related

How to structure Sphinx documentation to prevent overriding documentation if files have same name

I have multiple small projects that are somewhat related to eachother so i want them to be in single sphinx documentation.
Each handler.py module have different docstring but when i build documentation both modules reference handler.py from App1.
Each App is single project but containing similar file names:
/
├─ conf.py
├─ index.rst
├─ App1/
│ ├─ handler.py
│ ├─ errors.py
│ ├─ DOCS/
│ │ ├─ modules.rst
│ │ ├─ handler.rst
│ │ ├─ errors.rst
├─ App2/
│ ├─ DOCS/
│ │ ├─ modules.rst
│ │ ├─ handler.rst
│ ├─ handler.py
in root "conf.py" i insert paths for them:
sys.path.insert(0, os.path.abspath("App1/"))
sys.path.insert(0, os.path.abspath("App2/"))
in root "index.rst" i include them in toctree:
.. toctree::
:maxdepth: 12
:caption: Content:
app1/docs/modules
app2/docs/modules
each hander.rst contains:
handler module
================
.. automodule:: handler
:members:
:undoc-members:
:show-inheritance:
How should i structure or reference python files so that each App.handler page contains docstring only from that app handler.py?

Django rest framwork folder structure

i found this question in stackoverflow link.
but I still haven't received the answer to my question, what exactly is the standard project structure in Django?
my_project/
├─ my_project/
│ ├─ settings.py
│ ├─ wsgi.py
│ ├─ asgi.py
│ ├─ __init__.py
│ ├─ urls.py
├─ my_app/
│ ├─ __init__.py
│ ├─ urls.py
│ ├─ models.py
│ ├─ views.py
│ ├─ admin.py/
│ ├─ apps.py
│ ├─ tests.py
Does that answer your question?
If not, please write down what you do not understand

Cannot import submodule from Python tests module

I have the following project structure:
my-app/
├─ README.md
├─ LICENSE
├─ tests/
│ ├─ my_test.py
├─ src/
│ ├─ utils/
│ │ ├─ __init__.py
│ │ ├─ utils.py
│ ├─ __init__.py
│ ├─ app.py
And my app.py (located in src) uses some functions of utils.py. When I run python src/app.py it works fine, but, when I run python -m unittest tests.my_tests for the tests code (which instantiates the main class at app.py) I get the following error:
from utils.utils import *
ModuleNotFoundError: No module named 'utils'

Python: 'ModuleNotFoundError' when trying to import module from files

i want to access config/cache.py from test.py And ,I need internal access like from config/env.py to database/modules/game.py but i get this error always:
ModuleNotFoundError: No module named 'modules'
my project tree:
project
├─ modules
│ ├─ cache
│ │ ├─ playtime.py
│ │ └─ __init__.py
│ ├─ config
│ │ ├─ cache.py
│ │ ├─ database.py
│ │ ├─ env.py
│ │ ├─ yaml.py
│ │ └─ __init__.py
│ ├─ database
│ │ └─ models
│ │ ├─ game.py
│ │ ├─ member.py
│ │ ├─ room.py
│ │ ├─ wiki.py
│ │ └─ __init__.py
│ ├─ room
│ ├─ utils
│ │ ├─ functions.py
│ │ ├─ logger.py
│ │ └─ __init__.py
│ └─ __init__.py
├─ run.py
└─ test
└─ test.py
and inits file are like that:
from .config import *
from .utils import *
from .database import *
from .cache import *
Python automatically insert the folder in which the main script resides to sys.path, which is a list containing folders where to find packages and modules.
So, if test.py were in folder project, that folder would be in sys.path and then you could import the modules package and any of its subpackages or modules.
import module # main package module
from module import config # subpackage config
from module.config import env # module env
(Just in case, a module is a file ending in .py, and a package is a folder with a file called __init__.py inside and optionally subpackages and/or modules.)
So one solution wood be to move the file test.py to projects
Another alternative is to add the folder projects (as a str) manually to sys.path which is a simple python list before importing.
import sys
sys.append('.../projects')
from modules.config import env

Import local modules in Jupyter notebook

I would like to outsource some general functions useful for multiple notebooks in a module (also for testing purposes). The current directory structure looks like the following
jupyter/
├─ notebooks/
│ ├─ 01 Notebook 1.ipynb
│ ├─ ...
├─ src/
│ ├─ module_a/
│ │ ├─ __init__.py
│ │ ├─ func_a.py
│ ├─ module_b/...
├─ tests/...
├─ data/...
├─ .../
In func_a.py, there is a simple function def print_a(): print('a')
However, when I would like to import and use module_a in 01 Notebook 1.ipynb by using (what I think makes sense)
from .. src.module_a import print_a
I got an ImportError: attempted relative import with no known parent package. What am I doing wrong? I am using Python 3.9.
I would try to append the src directory to the system path like so:
import sys
sys.path.append("/path/to/your/src")
from src.module_a import a
please note that you can use the relative path from your notebook root and not absolute path like in the example above, so the following:
sys.path.append("src")
should work too

Categories