Define setuptools path for each import module - python

Given the following project structure:
root
└── dir1
├─ setup.py
├─ script1.py
├── processor
│ ├─ preprocessor.py
│ └─ postprocessor.py
│
├──src_dir
│ ├─ predictor.py
│ └─ script2.py
│
I would like the custom package to ONLY contain the two scripts inside the processor folder and the predictor.pyscript. Moreover, the scripts should be accessible via from processor.preprocessor import ... while the predictor to be in the main root and therefore importable as from predictor import ....
I I should then run the following setup script: python setup.py sdist --formats=gztar:
from setuptools import setup
setup(name="processors",
version="0.1",
...
)

Related

pytest no module named common

I'm trying to get started with python and pytest, I have the following project structure :
.
├── my_module
│ ├── __init__.py
│ ├── common
│ │ ├── __init__.py
│ │ └── utils.py
│ └── my_script.py
└── test
├── __init__.py
└── test_my_script.py
When I run tests (using pytest), I get an error:
no module named 'common'.
I have also the following all configs files:
tox.ini
setup.py
setup.cfg
pyproject.toml
someone know what I missed?
EDIT
here is how I import utils from test_my_script.py :
from common.util import func1,func2
common.util module is not accessible from your test execution because it is located in my_module package.
You should use import starting from working directory which is your project root.
Consider using following import instead:
from my_module.common.util import func1, func2

ROS2 separate directory for Python nodes

Is it possible to have a separate directory for Python nodes in ROS2 (foxy)?
python_pkg/
│
├─ python_pkg/
│ ├─ __init__.py
│ ├─ my_module.py
│ └─ hello_world.py
|
├─ nodes/
│ ├─ __init__.py
│ └─ simple_node.py
|
└─ ...
In setup.py I have tried adding:
setup(
name="python_pkg",
...
entry_points={
'console_scripts': [
'hello_world = python_pkg.hello_world:main',
'simple_node = nodes.simple_node:main',
],
},
)
If I source the workspace and run ros2 run python_pkg simple_node I get the error ModuleNotFoundError: No module named 'nodes'.

Why my test files from a test folder can't import the source files from a source folder?

I have a problem where this is my project structure:
.
├── Resources/
├── src/
│ ├── __init__.py
│ ├── main.py
│ ├── utils/
│ │ ├── __init__.py
│ │ ├── util.py
│ │ └── otherUtil.py
│ └── calculations/
│ ├── __init__.py
│ └── financials.py
└── tests/
├── __init__.py
└── test.py
My problem is that I can't reach the classes from the src/ folder from the tests, although the code in src/ can reach the Resources folder, through the first shown method.
I have tried:
To append the home library path this way:
Here I used the from src import util after these lines, I even tried from .src import util.
Then this way:
Here I used the from src import util after these lines, I even tried from .src import util.
Than without the sys.path.append() with no use.
I have tried every combination I know, but for no use, and I don't want to install them as individual packages. Does someone have an idea, witch will solve my problem?
Clarification edit:
I don't want to put the tests in the source folder, i want to keep them separate.
You can use this code found here:
# test.py
import sys
# insert at 1, 0 is the script path (or '' in REPL)
sys.path.insert(1, '/path/to/utils/')
import utils

cannot import name 'a' from 'tools'

I can run test_pkg1.py and test_pkg2.py
but when I run test.py
Error occurred at test_pkg1.py
Exception has occurred: ImportError
cannot import name 'a' from 'tools' (unknown location)
root
├─ pkg1
│ ├─ tools
│ │ ├─ __init__.py
│ │ └─ a.py
│ ├─ __init__.py.py
│ └─ test_pkg1.py
├─ pkg2
│ ├─ tools
│ │ ├─ __init__.py
│ │ └─ b.py
│ ├─ __init__.py.py
│ └─ test_pkg2.py
├─ test.py
└─ d.py
test_pkg1.py
from tools import a
test_pkg2.py
from tools import b
test.py
from pkg2 import test_pkg2
from pkg1 import test_pkg1
In Python, import paths are always relative to the directory where the main program is executed unless you explicitly use a relative import by preceding the package name with a dot, which then makes the interpreter look for the package in the same directory as the module where the import is made:
test_pkg1.py:
from .tools import a
You also need to create an __init__.py file in the directory where the main program runs in order for that directory to be considered a package under which relative imports can be made.

Proper setup.py creation

So I have a project which is structured as shown below:
project
├── src
│ ├── api
│ │ ├── __init__.py
│ │ └── api.py
│ ├── instance
│ │ ├── __init__.py
│ │ └── config.py
│ ├── packages
│ │ ├── __init__.py
│ │ └── app.py
├── tests
│ └── __init__.py
├── requirements.txt
├── README.md
├── .gitignore
└── setup.py
I am trying to create the setup.py in order to call the instance/config.py from the package/app.py. So I created the following setup.py:
from distutils.core import setup
from setuptools import setup, find_packages
setup(
name='project',
version='0.1dev0',
author='Author name',
author_email='my_email',
packages=['api', 'instance', 'packages'],
long_description=open('README.md').read()
)
But I get the following error:
...
warnings.warn(tmpl.format(**locals()))
package init file 'src\__init__.py' not found (or not a regular file)
error: package directory 'api' does not exist
----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
So whenever I try to call the instance/config.py from the packages/app.py I get the following error:
from instance import config
ModuleNotFoundError: No module named 'instance'
What do I need to do to the setup.py file to make it work? Should the structure of the project be altered somehow?
Thanks a lot in advance!
In a nutshell, you have to create a root package project in project directory. See https://packaging.python.org/tutorials/packaging-projects/ for more details. Then
from project.instance import config
Rename src to project and put there __init__.py with imports
from . import api, instance, packages

Categories

Resources