Skip to content

Instantly share code, notes, and snippets.

@franTarkenton
Last active February 28, 2024 18:13
Show Gist options
  • Save franTarkenton/ee7ec052a188f2461099e0667cba888f to your computer and use it in GitHub Desktop.
Save franTarkenton/ee7ec052a188f2461099e0667cba888f to your computer and use it in GitHub Desktop.

Intro

Each project / task / app / script that you work on may have its own dependency requirements. Ideally you don't want to have to "polute" your core python install with a bunch of dependent modules that will accumulate over time. This document is intended to describe how to manage these requirements.

https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/

Solution

Use virtualenv

Create a virtualenv that will contain the dependencies for your project. In the example below you will be creating a virtualenv called xl_ve

python -m virtualenv xl_ve

Use virtualenv w/ Windows subsystem for linux (ubuntu base)

virtualenv -p python3 wsl_ve2

Activate your virtualenv

xl_ve/Scripts/activate

Install Dependencies

python -m pip install openpyxl

point development environment to ve

If you are using eclipse/pydev then you would configure a brand new interpreter that points to the python.exe that is located in the virtualenv directory, in this example that would be:

xl_ve/Scripts/python.exe

Then make sure your project is configured to use that interpreter. http://pydev.blogspot.com/2010/04/pydev-and-virtualenv.html

moving forward...

Ideally your project should have a requirements.txt file in its root folder that not only describes the dependency but the version as well... example:

requests==2.16.0
openpyxl==2.6.2
twine==1.13.0

Then to install all your dependencies you would run

python -m pip install -r requirements.txt

You can also dynamically generate a requirements.txt based on what has already been installed into your existing virtualenv...

python -m pip freeze > requirements.txt

Other approaches

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment