Formulir Kontak

Nama

Email *

Pesan *

Cari Blog Ini

Create Python Venv

Create Virtual Environment using venv in Python

Introduction

A virtual environment is an isolated Python environment that allows you to install and run specific versions of Python and packages without affecting the system-wide Python installation. It helps in managing dependencies and isolating different projects, ensuring that each project has its own set of dependencies.

Advantages of Virtual Environments

  • Isolate project dependencies
  • Prevent version conflicts
  • Ensure reproducibility
  • Simplify dependency management

Creating a Virtual Environment

Python 3.3 and above include the venv module, which allows you to create a virtual environment.
python3 -m venv <env_name>
where "<env_name>" is the name you want to give to your virtual environment. Upon creation, a virtual environment folder is created with a "bin" directory containing Python and other scripts.

Activating a Virtual Environment

To use the virtual environment, you must activate it.
source <env_name>/bin/activate
You can verify the activation by checking the terminal prompt, which should now display the name of the virtual environment.

Deactivating a Virtual Environment

To exit the virtual environment and return to the system-wide Python installation, type:
deactivate

Installing Packages in a Virtual Environment

To install packages within the virtual environment, you can use pip as usual:
pip install <package_name>
This will install the package only within the active virtual environment.

Deleting a Virtual Environment

To remove a virtual environment, simply delete the folder containing it.


Komentar