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'
Related
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.
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?
I have a simple python package that I've published on GitHub. I installed the package locally on my machine using pip. I am trying to import a subfolder of the module but I keep getting a ModuleNotFoundError: No module named 'package_folder.subfolder1'
├── package_name/
│ ├── README.md
│ ├── setup.py
│ └── package_folder
│ ├── __init__.py
│ ├── file1.py
│ ├── file2.py
│ ├── subfolder1/
│ │ ├── __init__.py
│ │ ├── file11.py
│ │ └── file12.py
I have the __init__.py files in both directories, so I'm not sure why I am unable to access the subfolder1 files.
I am able to import file1.py and file2.py from the top-level package_folder with from package_folder import file1.py.
In the setup.py you have to include the subfolder in the packages as well. So, in setup.py instead of:
packages=['package_folder']
You have to do:
packages=['package_folder', 'package_folder/subfolder1']
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
I have a project structure, like so:
└── auto/
│ ├──── __init__.py
│ └──── __pycache__/
│ ├──── deploy
│ ├──── destroy
│ └──── helpers/
│ │ ├──── __init__.py
│ │ ├──── cli.py
│ │ └──── zip.py
│ └──── synth
└── src/
│ ├──── __init__.py
│ ├──── main.py
│ └──── models/
│ │ ├──── __init__.py
│ │ ├──── filter.py
│ │ └──── json_extract_config.py
│ └──── utils/
│ │ ├──── __init__.py
│ │ └──── json_extractor.py
I am trying to call the script synth in the ./auto/synth location.
The synth file is a script with a shebang at the top #!/usr/bin/env python3
It imports a function from the ./auto/helpers/zip.py file.
When I run, ./auto/synth from the root folder it throws an error:
from auto.helpers.zip import zip_folder
ModuleNotFoundError: No module named 'auto'
Although when I run PYTHONPATH="$PWD" ./auto/synth the script executes successfully.
Can someone help me understand this behaviour and how to avoid it so that my scripts can be run normally without having to change the PYTHONOPATH?
Thanks in advance.
If you are using python version 3.3+ you do not need to use the __init__.py files anymore, as the namespacing is built into the modules now. link --> Is __init__.py not required for packages in Python 3.3+
I believe you will have to add the {full path}/src and {full path}/auto directories to the PYTHONPATH to provide python scripts the proper pathing, or you can use the python sys module and use sys.path inside your modules to set your pathing for the module you would like to import link --> https://docs.python.org/3/tutorial/modules.html#the-module-search-path
import sys
# example path could be "C:/Users/Bob/Desktop/project/auto"
sys.path.append("{full path to your auto directory}/auto")