Getting familiar with processes and tools, which are required to develop a Python package.
Python Version Management (pyenv)
You should avoid 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, 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.
$ (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 OK
$ (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 ok ---------------------------------------------------------------------- Ran 5 tests in 3.277s OK
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