Skip to content

Instantly share code, notes, and snippets.

@ryumada
Last active November 14, 2024 07:56
Show Gist options
  • Save ryumada/c22133988fd1c22a66e4ed1b23eca233 to your computer and use it in GitHub Desktop.
Save ryumada/c22133988fd1c22a66e4ed1b23eca233 to your computer and use it in GitHub Desktop.
Python Virtual Environment Tutorial

Python Virtual Environment Tutorial

This is used to use a virtual environment to install packages instead of modifying the system-wide environment. You should create a virtual environment in every python app development to make your system clean and bug-free from bugged pip packages.

The virtual environment is also used to separate your packages in python app development. Normally, you will have different packages in one python app to another.

How to start Python Development with Python Environment

You can start by following this documentation to learn how to make it by clicking this link: https://docs.python.org/3/tutorial/venv.html

Or you can follow my simple instruction to create a virtual account until you can activate it. But I'm strongly recommend you to read that documentation.

1. Install python3-venv or python3.x-venv

If your system doesn't have venv library, install it using apt.

sudo apt install python3-venv

2. To create a virtual environment, decide which directory where you want to place it

You can create a new directory to start your new python app development.

3. Run venv module as a script in your chosen directory path

python3 -m venv tutorial-venv

That command will create a new tutorial-venv directory in your directory. The new directory contains a copy of Python interpreter and various supporting files.

Based on the documentation, you should create virtual environment directory called .venv. Read more about it in the documentation.

4. Activate your virtual environment

on Windows, run:

tutorial-env\Scripts\activate.bat

on Unix or MacOS, run:

source tutorial-env/bin/activate

After you run one of those command, the terminal or command prompt you used will show what virtual environment you are using.

20220519142606

5. You can start installing your packages

These commands is used to manage pip packages.

1. Installing a package:

through python command:

python -m pip install package-name

or by directly using the pip command:

pip install package-name
2. Installing a package using specific version

by giving the == followed by the version number after the package name.

pip install package-name==2.6.9
3. Upgrading a package
pip install --upgrade package-name
4. Uninstalling a package
pip uninstall package-name
5. Showing the details of a package
pip show package-name
6. Listing all packages installed in your environment
pip list
7. Exporting installed packages to requirement.txt file
pip freeze > requirement.txt

This will create a file called requirement.txt. That file contains a list of the packages you installed in your virtual environment (venv).

8. Installing packages from exported requirement.txt
pip install -r requirement.txt

This will run pip install package-name repeatedly as many the packages listed in requirement.txt.

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