About
Code Versioning - Release and Code Shipping - Deployment in Python
How to release regular package adapted from https://packaging.python.org/tutorials/packaging-projects/ (they are using the setuptools framework)
Articles Related
Steps
With python 3.
Structure
/project_home
/hello_world_pkg
__init__.py
setup.py
where:
- setup.py is the package information file setup.py with a package name as hello-nico because it's not exist at test.pypi.org repo.
name = "hello_nico"
Building a distribution
Update the building tool
- run this command from the package directory (where setup.py is located) ???
python -m pip install --user --upgrade setuptools wheel
where:
- --user will install to the Python user install directory for your platform. See location
- --upgrade will upgrade the package
- setuptools is Python 3 - SetupTools
Build
cd ./project
python setup.py sdist bdist_wheel
where:
- sdist will create a source distribution of the current project
- bdist_wheel will create a wheel
This will create 3 sub directories:
- build
- dist
- hello_nico.egg-info
The most important are in the dist subdirectory:
dist/
hello_nico_pkg-0.0.1-py3-none-any.whl
hello_nico_pkg-0.0.1.tar.gz
where:
- The tar.gz file is a source archive
- The .whl file is a built distribution in the wheel format.
Note: the full output of the command is:
running sdist
running egg_info
writing hello_nico.egg-info\PKG-INFO
writing dependency_links to hello_nico.egg-info\dependency_links.txt
writing top-level names to hello_nico.egg-info\top_level.txt
reading manifest file 'hello_nico.egg-info\SOURCES.txt'
writing manifest file 'hello_nico.egg-info\SOURCES.txt'
running check
creating hello-nico-0.0.2
creating hello-nico-0.0.2\hello_nico.egg-info
creating hello-nico-0.0.2\hello_nico_pkg
copying files to hello-nico-0.0.2...
copying README.md -> hello-nico-0.0.2
copying setup.py -> hello-nico-0.0.2
copying hello_nico.egg-info\PKG-INFO -> hello-nico-0.0.2\hello_nico.egg-info
copying hello_nico.egg-info\SOURCES.txt -> hello-nico-0.0.2\hello_nico.egg-info
copying hello_nico.egg-info\dependency_links.txt -> hello-nico-0.0.2\hello_nico.egg-info
copying hello_nico.egg-info\top_level.txt -> hello-nico-0.0.2\hello_nico.egg-info
copying hello_nico_pkg\__init__.py -> hello-nico-0.0.2\hello_nico_pkg
Writing hello-nico-0.0.2\setup.cfg
creating dist
Creating tar archive
removing 'hello-nico-0.0.2' (and everything under it)
running bdist_wheel
running build
running build_py
installing to build\bdist.win32\wheel
running install
running install_lib
creating build\bdist.win32\wheel
creating build\bdist.win32\wheel\hello_nico_pkg
copying build\lib\hello_nico_pkg\__init__.py -> build\bdist.win32\wheel\.\hello_nico_pkg
running install_egg_info
Copying hello_nico.egg-info to build\bdist.win32\wheel\.\hello_nico-0.0.2-py3.7.egg-info
running install_scripts
creating build\bdist.win32\wheel\hello_nico-0.0.2.dist-info\WHEEL
creating 'dist\hello_nico-0.0.2-py3-none-any.whl' and adding 'build\bdist.win32\wheel' to it
adding 'hello_nico_pkg/__init__.py'
adding 'hello_nico-0.0.2.dist-info/METADATA'
adding 'hello_nico-0.0.2.dist-info/WHEEL'
adding 'hello_nico-0.0.2.dist-info/top_level.txt'
adding 'hello_nico-0.0.2.dist-info/RECORD'
removing build\bdist.win32\wheel
Upload
Create an account to the repo
Create an account on Test PyPI.
Update the deploy tool
- twine upload the archive to Test PyPI
python -m pip install --user --upgrade twine
REM Optional Add the scripts location to the path
set PATH=C:\Users\gerard\AppData\Roaming\Python\Python37\Scripts;%PATH%
Deploy
- upload all of the archives under the dist sub-directory.
twine upload --repository-url https://test.pypi.org/legacy/ dist/*
Enter your username: gerardnico
Enter your password:
Uploading distributions to https://test.pypi.org/legacy/
Uploading hello_nico-0.0.1-py3-none-any.whl
100%| 4.48k/4.48k [00:01<00:00, 2.88kB/s]
Uploading hello nico-0.0.1.tar.gz
100%| 4.23k/4.23k [00:01<00:00, 3.16kB/s]
It may take a minute or two for your project to appear on the site.
The package can be then see at:
https://test.pypi.org/project/package-name
Tintin ! In our case: https://test.pypi.org/project/hello-nico
On the real index, you can see if your package has successfully uploaded by navigating to the URL https://pypi.org/project/
Use it
- Syntax
python -m pip install --index-url https://test.pypi.org/simple/ packageName
- Example with the package Hello Nico in a virtual environment
cd c:\tmp
mkdir venv
cd venv
Scripts\python -m pip install --index-url https://test.pypi.org/simple/ hello-nico
Looking in indexes: https://test.pypi.org/simple/
Collecting hello-nico
Downloading https://test-files.pythonhosted.org/packages/e2/37/eb9576a8b8b5a1881ace829edcb49e5a118ac10c50b3f78172b84a7efc8c/hello_nico-0.0.1-py3-none-any.whl
Installing collected packages: hello-nico
Successfully installed hello-nico-0.0.1
- The package was installed
ls Lib\site-packages\hello_nico-0.0.1.dist-info
INSTALLER METADATA RECORD WHEEL top_level.txt
Import to test
- Start Python
Scripts\python.exe
- Import the module and access the property defined in the init.py file
import hello_nico_pkg
hello_nico_pkg.name
'hello_nico'
Documentation / Reference