Packaging and Distributing Python Script
While working as a summer intern i was tasked to package a python script.This is how i did it.
Note: I am using vscode as ide and ubuntu 20.4 LTS as os.
Make a virtual environment and set your interpreter path to the virtual environment[1]
python3 -m venv ~/veOpen your python script and load all the required libraries and packages.
pip install scikit-learn
pip install pandas
pip install numpySet the file structure[2].
Create a folder src inside your package folder and place __init__.py file and your python script in it.
Get Shreshth Pal’s stories in your inbox
Join Medium for free to get updates from this writer.
/spatialcdr
~/src/__init__.py
~/src/spatialcdr
pyroject.toml
LICENSE
README.md
Fill metadata information in pyproject.toml [2]
[build-system]
requires = ["setuptools>=61.0"]
build-backend = "setuptools.build_meta"
[project]
name = "testspatialcdr"
version = "0.0.2"
authors = [
{ name="Ashish_Swain", email="swain.3@iitj.ac.in" },
]
maintainers = [
{name = "Shreshth Pal", email = "shreshthpal@outlook.com"}
]
description = "Spacer package for Spatial Transcriptomics"
readme = "README.md"
license = { file="LICENSE" }
requires-python = ">=3.8"
classifiers = [
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
]
dependencies = [
"pandas>=1.3.5",
"scikit-learn==1.1.1",
"numpy>=1.17.3"
]
[project.urls]
"Homepage" = "https://github.com/swainasish/Spectacular"
"Bug Tracker" = "https://github.com/swainasish/Spectacular"Manage project dependencies
- Try using ≥ instead of the ^ sign when defining dependencies.
- Set to the oldest version specified if mutually dependent dependency exists.
- If finding it difficult to manage dependencies try using poetry for this task.
Create Distribution files [2]
A new folder dist will be created once you run this command you can keep
python3 -m pip install --upgrade build
python3 -m buildUpload distribution files[2]
There are two ways to do this
- using token:
username:__token__
password: pypi-#######
2. using pypi id.
python3 -m pip install --upgrade twine
python3 twine upload dist/*References
[1] https://docs.python.org/3/library/venv.html
[2]https://packaging.python.org/en/latest/tutorials/packaging-projects/










