Python - Regular Package (Shipping | Packaging)

> Procedural Languages > Python > Python - (Shipping|Packaging)

1 - About

Advertising

3 - Steps

With python 3.

3.1 - Structure

/project_home
   /hello_world_pkg
       __init__.py
  setup.py

where:

__init__.py
name = "hello_nico"

3.2 - Building a distribution

3.2.1 - 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:

Advertising

3.2.2 - Build

cd ./project
python setup.py sdist bdist_wheel

where:

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:

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
Advertising

3.3 - Upload

3.3.1 - Create an account to the repo

Create an account on Test PyPI.

3.3.2 - 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%

3.3.3 - 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/<projecName>

3.3.4 - Use it

  • Syntax
python -m pip install --index-url https://test.pypi.org/simple/ packageName
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

3.4 - 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'

4 - Documentation / Reference