Getting familiar with processes and tools, which are required to develop a Python package.


  1. You've gone through Create A Package
  2. Install pyenv

Getting Started

Python Version Management (pyenv)

You should avoid running Python with python3 and pip3; it's confusing and inconsistent. You should always use a clean python and pip, and let pyenv do the heavy-lifting for choosing the right Python version.

pyenv lets you easily switch between multiple versions of Python. It's simple, unobtrusive, and follows the UNIX tradition of single-purpose tools that do one thing well.

pyenv is great for checking backward compatibility; for example, I have Python 3.8.2 installed, and I'd like to check if my Python package runs properly on Python 3.6.7, which is a classic use-case.

Switching To A Different Python Version

  1. Install the relevant Python version - pyenv install 3.7.7
  2. Run export PYENV_VERSION=3.7.7
  • For a day to day use

    1. Install relevant version - pyenv install 3.8.2
    2. Add export PYENV_VERSION=3.8.2 to your terminal's rc or _profile ($HOME/.bashrc, $HOME/.bash_profile, $HOME/.zshrc)

Full Example Of pyenv

(python-project) $ export PYENV_VERSION=
(python-project) $ pyenv versions
* system (set by /Users/meirgabay/.pyenv/version) # default OS Python
(python-project) $ python --version
Python 2.7.16

# Switching to a different version
(python-project) $ export PYENV_VERSION=3.7.7
(python-project) $ python --version
Python 3.7.7

# Day to day use
# Add export PYENV_VERSION=3.8.2 to your ~/.bash_profile
(python-project) $ source ~/.bash_profile
(python-project) $ python --version
Python 3.8.2

Python Virtual Environments

If this is the first time you hear this term, I suggest you go read about the venv package.

Using virtual environments isolates the package's requirements (packages) from the packages installed on your machine.

(python-project) $ python -m venv ENV                   # create virtual environment
(python-project) $ source ./ENV/bin/activate            # get in virtual environment
(ENV) (python-project) $ pip install requests pillow    # install dependencies
(ENV) (python-project) $ pip freeze > requirements.txt  # generate a list of required packages with version constraints
(ENV) (python-project) $ deactivate                     # get out of virtual environment
(python-project) $                                      # we're out of the virtual environment

Install the Package From Source Code

It's possible to install the package from Source code, though each time your code changes, you'll need to re-install the package to test the newly added features.

$ (python-project) pip install .
Processing /Users/meirgabay/python-project
Building wheels for collected packages: unfor19-appy
Successfully installed unfor19-appy-0.0.1

Install The Package In Development Mode

Adding the --editable flag enables checking the latest changes of your code without re-installing the package.

$ (python-project) pip install --editable .

You don't need to do pip install . after each time you change the source code. Run pip install --editable . once, and start developing your Python package!

Test The Package

It is recommended to write tests to ensure that everything is working as expected and that the new features that you add work along with the current features.

The most popular tools for writing tests in Python are unittest (built-in) and pytest (pip). Read more about the differences between them in pytest-vs-unittest.

To keep things simple, I wrote the tests/ in this project with unittest. The following command will discover the test files in tests/ and execute them.

  • Clean Output

    $ (python-project) python -m unittest discover -s tests
    ....Created the file: C:\Users\unfor19\python-project\.meirg-ascii-test.txt
    Ran 5 tests in 3.510s
  • Verbose output

    $ (python project) python -m unittest discover -s tests --verbose
    test_get_fact (test_api.ApiTestCase) ... ok
    test_firstname (test_greet.GreetTestCase) ... ok
    test_fullname (test_greet.GreetTestCase) ... ok
    test_number (test_greet.GreetTestCase) ... ok
    test_img_to_ascii (test_img_to_ascii.ApiTestCase) ... Created the file: C:\Users\unfor19\python-project\.meirg-ascii-test.txt
    Ran 5 tests in 3.277s

Run The Package

  • Run appy as a CLI

    $ appy
    Insert your name: willy
    Hello willy
  • Run appy as a Python module

    $ python -m appy
    My Path: .pyenv/versions/3.8.2/Python.framework/Versions/3.8/lib/python3.8/site-packages/unfor19_appy-0.0.1-py3.8.egg/appy/__main__.py
    Insert your name: willy
    Hello willy

Import the Package

  • Import appy as a package

    $ python
    >>> from appy.utils import message
    >>> message.greet("willy")
    Hello willy

Uninstall The Package

$ pip uninstall -y unfor19-appy

Found existing installation: unfor19-appy 0.0.1
Uninstalling unfor19-appy-0.0.1:
  Successfully uninstalled unfor19-appy-0.0.1

This blog post is part of the Python Project series, and is based on this GitHub repository - unfor19/python-project.
The GitHub repo includes an example of a Python project, and Wiki Pages that describe the necessary steps for developing, creating and distributing a Python package.

Originally published at github.com/unfor19/python-project on 11 November, 2020

Recommended Posts

No comment yet, add your voice below!

Add a Comment

Your email address will not be published. Required fields are marked *