setuptools Python
Introduction
find_packages: finding packages
To see which packages setuptools detects, run
python >>> import setuptools >>> setuptools.find_packages()
['src']
This information is useful if you are going to pass arguments to setup.py → packages
Project structure
pac_container └── pac ├── __init__.py ├── pcalc │ ├── calculator.py │ └── __init__.py ├── printer │ ├── __init__.py │ └── printer.py └── setup.py
It is important to place the file correctly setup.py
In this case, after installing the package with pip its name will be pac, and to import it you need to
use pcalc and printer
If there are several such directories as src next to each other, then several packages will be available for import, but
there will be one pac in pip for all of them
# pac/src/calculator.py def add(a, b): return a + b def subtract(a, b): return a - b def multiply(a, b): return a * b def divide(a, b): return a / b
# pac/setup.py # Should be outside source code import setuptools setuptools.setup( include_package_data=True, name="pac", version="1.0.0", description="setuptools demo pac", url="https://www.eth1.ru/dev/python/packages/setuptools/", author="www.eth1.ru", author_email="info@eth1.ru", packages=setuptools.find_packages(), install_requires=["pandas", "pytest"], long_description="setuptools demo package by www.eth1.ru", long_description_content_type="text/markdown", classifiers=[ "Programming Language :: Python :: 3", "Operating System :: OS Independent", ], )
# pac/printer/printer.py def printer(): print("This is printer")
cd pac python >>> import setuptools >>> setuptools.find_packages()
['printer', 'pcalc']
python -m pip freeze
python -m pip install .
Processing /home/andrei/pac_container/pac DEPRECATION: A future pip version will change local packages to be built in-place without first copying to a temporary directory. We recommend you use --use-feature=in-tree-build to test your packages with this new behavior before it becomes the default. pip 21.3 will remove support for this functionality. You can find discussion regarding this at https://github.com/pypa/pip/issues/7555. Collecting pandas Using cached pandas-1.4.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (11.7 MB) Collecting pytest Using cached pytest-7.1.2-py3-none-any.whl (297 kB) Collecting pytz>=2020.1 Using cached pytz-2022.2.1-py2.py3-none-any.whl (500 kB) Collecting numpy>=1.18.5 Using cached numpy-1.23.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (17.1 MB) Collecting python-dateutil>=2.8.1 Using cached python_dateutil-2.8.2-py2.py3-none-any.whl (247 kB) Collecting six>=1.5 Using cached six-1.16.0-py2.py3-none-any.whl (11 kB) Collecting tomli>=1.0.0 Using cached tomli-2.0.1-py3-none-any.whl (12 kB) Collecting attrs>=19.2.0 Using cached attrs-22.1.0-py2.py3-none-any.whl (58 kB) Collecting iniconfig Using cached iniconfig-1.1.1-py2.py3-none-any.whl (5.0 kB) Collecting packaging Using cached packaging-21.3-py3-none-any.whl (40 kB) Collecting pluggy<2.0,>=0.12 Using cached pluggy-1.0.0-py2.py3-none-any.whl (13 kB) Collecting py>=1.8.2 Using cached py-1.11.0-py2.py3-none-any.whl (98 kB) Collecting pyparsing!=3.0.5,>=2.0.2 Using cached pyparsing-3.0.9-py3-none-any.whl (98 kB) Using legacy 'setup.py install' for pac, since package 'wheel' is not installed. Installing collected packages: six, pyparsing, tomli, pytz, python-dateutil, py, pluggy, packaging, numpy, iniconfig, attrs, pytest, pandas, pac Running setup.py install for pac ... done Successfully installed attrs-22.1.0 iniconfig-1.1.1 numpy-1.23.2 pac-1.0.0 packaging-21.3 pandas-1.4.3 pluggy-1.0.0 py-1.11.0 pyparsing-3.0.9 pytest-7.1.2 python-dateutil-2.8.2 pytz-2022.2.1 six-1.16.0 tomli-2.0.1
python -m pip freeze
attrs==22.1.0 iniconfig==1.1.1 numpy==1.23.2 pac==1.0.0 packaging==21.3 pandas==1.4.3 pluggy==1.0.0 py==1.11.0 pyparsing==3.0.9 pytest==7.1.2 python-dateutil==2.8.2 pytz==2022.2.1 six==1.16.0 tomli==2.0.1
To demonstrate that the installed package can be used from any directory, let us go to the home directory.
cd
pwd
/home/andrei
python >>> import pcalc >>> from pcalc import calculator >>> calculator.divide(5, 2)
2.5
>>> from printer import printer >>> printer.printer()
This is printer
Article author: Andrei Olegovich