Top Rated Plus on Upwork with a 100% Job Success ScoreView on Upwork
retzdev logo
logo
Building a Python Package

Building a Python Package: Virtual Environment (3)

by Jarrett Retz

January 26th, 2021

    Update

    I've started to write the core module for the BEA project. The package is an API client, so it's going to need to make HTTP requests. The most popular library in Python for this purpose is requests.

    Therefore, I needed to set up a way to manage my package dependencies. This article goes over how to manage dependencies with pipenv.

    My current project directory looks like the below tree diagram.

    .
    ├── LICENSE
    ├── Pipfile
    ├── Pipfile.lock
    ├── README.md
    ├── bea
    │   ├── __init__.py
    │   ├── base.py
    │   ├── bea.py
    │   └── tables
    │       ├── __pycache__
    │       │   ├── tables.cpython-38.pyc
    │       │   └── tables.cpython-39.pyc
    │       └── tables.py
    ├── setup.py
    └── tests

    Virtual Environment with pipenv

    Since the last article, I created a virtual environment for managing my package dependencies. That's why there is now a Pipfile and Pipfile.lock file.

    To do this, first, you need to install pipenv globally, or in your current virtual environment.

    pip install pipenv

    Then, I changed directories (in my terminal) into my project and ran the command:

    pipenv install requests

    This will produce a lot of output. Furthermore, it created a new virtual environment that I activated in my shell (I named mine bea). The virtual environment that is active can be identified at the beginning of the command-line.

    In the code below, I am running the sub-environment (bea) under my default environment (base). This means, that when I execute files, it will use the packages (and versions) installed in my sub-environment.

    (bea) (base) jarrettretz@MacBook-Pro bea % 

    Run Scripts with the Environment

    For example, if I wanted to run a file in this module (using the active virtual environment) I would execute the command:

    pipenv run python3 [file_path]

    Conclusion

    We should use the installation statement for all the packages that we plan to use as dependencies for our module.

    pipenv install [module_name]

    To learn more about managing dependencies, and possible alternatives, check out Python's official guide.

    Next Article

    In this next article, I plan to dive into developing the module locally. Also—possibly—doing a development release. See you there!

    Jarrett Retz

    Jarrett Retz is a freelance web application developer and blogger based out of Spokane, WA.

    jarrett@retz.dev

    Subscribe to get instant updates

    Contact

    jarrett@retz.dev

    Legal

    Any code contained in the articles on this site is released under the MIT license. Copyright 2024. Jarrett Retz Tech Services L.L.C. All Rights Reserved.