You should be aware of the many different methods to install your python packaging. I go over these here – come on, let’s take a look.

Prerequisite: Test Package ‘pkgexample’

To play along with these examples you can use the ‘pkgexample’ package we build previously, and also published on pypi.org

You can clone ‘pkgexample’ from the following repository.


https://github.com/RexBytes/pkgexample
0 forks.
0 stars.
0 open issues.

Recent commits:

Once you have cloned the repository you should have this on your local machine.

[email protected]:~/myrepos/rex$ tree -a pkgexample/

pkgexample/
├── .gitignore
├── LICENCE
├── pyproject.toml
├── README.md
└── src
    └── pkgexample
        ├── helloworld.py
        └── __init__.py

2 directories, 6 files
[email protected]:~/myrepos/rex$

Method 1: Standard Package Installation

We’re going to install ‘pkgexample’ direct from the pypi.org repository with the following command.

python3 -m pip install pkgexample

Installation

Here is what you should see in your terminal if you run this install command.

[email protected]:~$ python3 -m pip install pkgexample
Defaulting to user installation because normal site-packages is not writeable
Collecting pkgexample
  Using cached pkgexample-1.0.1-py3-none-any.whl (3.3 kB)
Installing collected packages: pkgexample
Successfully installed pkgexample-1.0.1
[email protected]:~$

Uninstallation

If you are going to install, you are probably going to want to uninstall at some point too. Here is the command to do that, and it’s output.

python3 -m pip uninstall pkgexample
[email protected]:~$ python3 -m pip uninstall pkgexample
Found existing installation: pkgexample 1.0.1
Uninstalling pkgexample-1.0.1:
  Would remove:
    /home/ubuntu/.local/lib/python3.10/site-packages/pkgexample-1.0.1.dist-info/*
    /home/ubuntu/.local/lib/python3.10/site-packages/pkgexample/*
Proceed (Y/n)? Y
  Successfully uninstalled pkgexample-1.0.1
[email protected]:~$

Method 2: Install A Locally Compiled Wheel Package

Package Building

Before we can demonstrate this installation method we need to build our ‘pkgexample’ package. An in depth guide to package building is available here.

[email protected]:~/myrepos/rex$ tree -a pkgexample/

pkgexample/
├── .gitignore
├── LICENCE
├── pyproject.toml
├── README.md
└── src
    └── pkgexample
        ├── helloworld.py
        └── __init__.py

2 directories, 6 files
[email protected]:~/myrepos/rex$

In short, to build the ‘pkgexample’ you have cloned from the repository, run the following command from the root of the cloned repository.

python3 -m build

You should see some output like this. You will see a lot more output scrolling, I have left that out here. The beginning and end statements should match though.

[email protected]:~/myrepos/rex/pkgexample$ python3 -m build
* Creating venv isolated environment...
* Installing packages in isolated environment... (setuptools>=65.0)
* Getting dependencies for sdist...
...
...
Successfully built pkgexample-1.0.1.tar.gz and pkgexample-1.0.1-py3-none-any.whl
[email protected]:~/myrepos/rex/pkgexample$

The build command will create a distributions directory ./dist/ containing your freshly build python wheel package, and a tar.gz version.

[email protected]:~/myrepos/rex/pkgexample$ ls ./dist/
pkgexample-1.0.1-py3-none-any.whl  pkgexample-1.0.1.tar.gz
[email protected]:~/myrepos/rex/pkgexample$

Installation

We are going to use the following command to pick out, and install the package wheel file.

python3 -m pip install ./dist/pkgexample-1.0.1-py3-none-any.whl

You should see a similar output to the following.

[email protected]:~/myrepos/rex/pkgexample$ python3 -m pip install ./dist/pkgexample-1.0.1-py3-none-any.whl
Defaulting to user installation because normal site-packages is not writeable
Processing ./dist/pkgexample-1.0.1-py3-none-any.whl
Installing collected packages: pkgexample
Successfully installed pkgexample-1.0.1
[email protected]:~/myrepos/rex/pkgexample$

Your locally compiled ‘pkgexample’ is now installed.

Uninstallation

To uninstall you use the same uninstall command as before.

python3 -m pip uninstall pkgexample

Method 3: Installation By Email

I wanted to connect some dots here and make you aware of the following.

You know that “*.whl” file that was created in the ./dist directory?

/myrepos/rex/pkgexample/dist/pkgexample-1.0.1-py3-none-any.whl

You can email that file directly to your best friend, or sell it on your e-store, or even transfer it by FTP.
Once you have a ‘*.whl‘ file in your possession you can install the package as follows.

[email protected]:~/my_wheels$ python3 -m pip install pkgexample-1.0.1-py3-none-any.whl

Great eh? Start creating packages and sharing them.

Method 4: Interactive Editable Installation For Development

Now, this should be as simple as running the following command from the root directory of your “src/source layout” repository.

python3 -m pip install -e .

It sort of is. At least it will be when they smooth over the rough edges. For now, please read this guide on how to install your package in editable mode.

One thought on “Bumper Python Package Installation Guide”
%d bloggers like this: