-
Notifications
You must be signed in to change notification settings - Fork 10
Package Development Process
Getting familiar with processes and tools, which are required to develop a Python package.
- You've gone through Create A Package
- Install 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.
- Install the relevant Python version -
pyenv install 3.7.7 - Run
export PYENV_VERSION=3.7.7
-
For a day to day use
- Install relevant version -
pyenv install 3.8.2 - Add
export PYENV_VERSION=3.8.2to your terminal'srcor_profile($HOME/.bashrc,$HOME/.bash_profile,$HOME/.zshrc)
- Install relevant version -
(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.2If 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 environmentIt'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.1Adding 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!
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 OK
-
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 ok ---------------------------------------------------------------------- Ran 5 tests in 3.277s OK
-
Run
appyas a CLI$ appy Insert your name: willy Hello willy
-
Run
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
appyas a package$ python ... >>> from appy.utils import message >>> message.greet("willy") Hello willy
$ 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- Home
- Python
- Docker
- Intro
- Build A Docker Image
- Multistage Build
- CI/CD
- Intro
- GitHub Actions