Python environment

Rating & reviews (0 reviews)
Study notes

Build a python app in isolation.

Solution: virtual environment
Self-contained copy of everything needed to run your program. This includes the Python interpreter and any libraries your program needs. By using a virtual environment, you can ensure your program will have access to the correct versions and resources to run correctly.

  1. Create a virtual environment that won't affect the rest of the machine.
  2. Step into the virtual environment, where you specify the Python version and libraries that you need.
  3. Develop your program.
1. Create a virtual environment
Create & go to app folder.
python -m venv env

Result a folders structure (may vary)

|__ /Scripts
Don't put your program files in the env directory. We suggest that you put your files in the srcdirectory or similar.

1.1. Activate the virtual environment

(env) in front of prompt

To desactivateL
No more (env) in front of prompt

1.2. Install a package
pip install python-dateutil

Once installed you will see

To see what packages are now installed in your virtual environmen
pip freeze

More ways to install a package:
  • Have a set of files on your machine and install from that source
    cd <to where the package is on your machine>
    python3 -m pip install .
  • Install from a GitHub repository
  • Install from a compressed archive file
    python3 -m pip install package.tar.gz
1.3. Use an installed package
from datetime import *
from dateutil.relativedelta import *
now =
now = now + relativedelta(months=1, weeks=1, hour=10)

Work with project files
Distribute project to others for collaboration.

Create a project file
pip freeze > requirements.txt
Creates a requirements.txtfile with all the packages that the program needs.
Create a .gitignorefile, and check in your application code and requirements.txt.
Check in the code to GitHub.
Publish to GitHub
Check Online
Result - all files from requirements.txt are online (GitHub)

Consume a project
Create a folder
Go into
Get the git URL from GitHub, and run:
git clone

We have the prject locally.

Install requirements.
pip install -r requirements.txt

Update/Run your all (what is in src).

Manage dependencies
Kee upgrading your packages:
  • Bug fixes.
    The library that you use might have problems. For example, a feature doesn't work as intended and the author goes in to fix it. You most likely want to upgrade the package as soon as such a fix is in place.
  • Security issues.
    Your package might have a security vulnerability. After such a fix is released, you want to upgrade the package to protect your company and your customers.
  • Additional features.
    The release of more features is nice, though it isn't a strong reason to upgrade the package. Still, if there's a feature that you've been waiting for, you might want to upgrade for that reason
Install the latest version
Specific version
pip install python-dateutil===1.4
Check if version is available
pip install python-dateutil===randomwords
ERROR: Could not find a version that satisfies the requirement python-dateutil===randomwords (from versions: 1.4, 1.4.1, 1.5, 2.1, 2.2, 2.3, 2.4.0, 2.4.1, 2.4.1.post1, 2.4.2, 2.5.0, 2.5.1, 2.5.2, 2.5.3, 2.6.0, 2.6.1, 2.7.0, 2.7.1, 2.7.2, 2.7.3, 2.7.4, 2.7.5, 2.8.0, 2.8.1, 2.8.2)
ERROR: No matching distribution found for python-dateutil===randomwords
pip freeze

Install last one
pip installpython-dateutil===2.8.2

pip install python-dateutil --upgrade

Versioning plain English:
  • The leftmost number is called Major. If this number increases, many things have changed, and you can no longer assume that methods are named the same or have the same number of arguments as before.
  • The middle number is called Minor. If it changes, a feature has been added.
  • The rightmost number is called Patch. If this number increases, it most likely means that a bug has been corrected.
Upgrade specific version to last one
pip install "python-dateutil==2.8.*" --upgrade

Clean up unused packages.

Remove one package only:
pip uninstall python-dateutil

Remove all installed packages, by first writing them to a requirements.txt list and then removing all packages in that list.
pip freeze > requirements.txt
pip uninstall -r requirements.txt -y

Now if you run pip freeze, you see that it contains only the following output: