Pyproject.toml#
The primary way to configure the process of building Python packages is by specifying the pyproject.toml
configuration file. There are several features you may encounter.
Dependencies#
With project.dependencies
variable you can specify which packages your packages depends on - so they’ll automatically installed by pip.
The following cell creates pyproject.toml
which requires python-dotenv==1.0.1
and installs “package” based on this pyproject.toml
.
[ -d /tmp/dependencies ] && rm -r /tmp/dependencies
mkdir /tmp/dependencies
cd /tmp/dependencies
cat << EOF > pyproject.toml
[project]
name = "toy_package"
version = "0.0.0"
dependencies = ["python-dotenv==1.0.1"]
EOF
mkdir target
pip3 install . --target target &> /dev/null
The following cell shows the contents of the installation target, there are files belonging to python_dotenv
that depend on it.
ls target
bin dotenv python_dotenv-1.0.1.dist-info toy_package-0.0.0.dist-info
Extra files#
If your package contains some resources that do not have the py
extension by default, they will not be included in the final distribution. You have to make extra configuration to make thing work.
The following cell creates an example that will be used to demonstrate the feature.
[ -d /tmp/data_files ] && rm -r /tmp/data_files
mkdir /tmp/data_files
cd /tmp/data_files
mkdir -p src/my_files
touch src/my_files/data_file{1..5}.txt
touch src/my_files/script_file{1..5}.py
tree src
cat << EOF > pyproject.toml
[project]
name = "toy_package"
version = "0.0.0"
EOF
src
└── my_files
├── data_file1.txt
├── data_file2.txt
├── data_file3.txt
├── data_file4.txt
├── data_file5.txt
├── script_file1.py
├── script_file2.py
├── script_file3.py
├── script_file4.py
└── script_file5.py
1 directory, 10 files
src
folder contains some .txt
and some .py
files.
The following cell installs the package in the result
folder and lists its contents:
[ -d result ] && rm -r result
mkdir result
pip3 install . --target result &> /dev/null
ls result/my_files
__pycache__ script_file2.py script_file4.py
script_file1.py script_file3.py script_file5.py
As the result there are only .py
files comes with installed package.
The following cell shows options to correct the configuration saving .txt
files in distibution.
cat << EOF > pyproject.toml
[project]
name = "toy_package"
version = "0.0.0"
[tool.setuptools]
include-package-data = true
[tool.setuptools.packages.find]
where = ["src"]
EOF
cat << EOF > MANIFEST.in
include src/my_files/*.txt
EOF
Only after installing the package with the new configuration do we get all the files needed in the distribution.
[ -d result ] && rm -r result
mkdir result
pip3 install . --target result &> /dev/null
ls result/my_files
data_file1.txt data_file4.txt script_file1.py script_file4.py
data_file2.txt data_file5.txt script_file2.py script_file5.py
data_file3.txt __pycache__ script_file3.py