Python Project - Package Development Process
Getting familiar with processes and tools, which are required to develop a Python package.
Python Version Management (pyenv)
You should avoid using running Python with
pip3, it's confusing and inconsistent. You should always use a clean
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
- Install the relevant Python version -
pyenv install 3.7.7
For a day to day use
- Install relevant version -
pyenv install 3.8.2
export PYENV_VERSION=3.8.2to your terminal's
- Install relevant version -
Full Example Of pyenv
(python-project) $ export PYENV_VERSION= (python-project) $ pyenv versions * system (set by /Users/meirgabay/.pyenv/version) # default OS Python 3.7.7 3.8.2 (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, then I suggest you go read about the venv package.
Using virtual environments assists with isolating the package's requirements (packages) from the packages which are installed on your machine.
(python-project) $ python -m venv ENV (python-project) $ source ./ENV/bin/activate (ENV) (python-project) $ pip install . (ENV) (python-project) $ pip freeze > requirements.txt # generate a list of required packages with version constraints (ENV) (python-project) $ deactivate (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 that your code changes, you'll need to re-install the package to test the newly added features.
$ 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.
$ pip install --editable .
You don't need to do
pip install . after each time you change the source code. Simply run
pip install --editable . once, and start developing your Python package!
Run The Package
appyas a CLI
$ appy Insert your name: willy Hello willy
appyas 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
appyas 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