Using venv in Python

Using venv in Python allows you to create a virtual environment, which is an isolated environment to manage dependencies for a specific project. Here's how you can use it step by step:


1. Create a Virtual Environment

  1. Open your terminal or command prompt.
  2. Navigate to the folder where you want to create the virtual environment.
  3. Run the following command:
    python -m venv myenv
    
    • Replace myenv with the name you want for your virtual environment.
    • This will create a directory named myenv containing the virtual environment.

2. Activate the Virtual Environment

Activation depends on your operating system:

  • On Windows:

    myenv\Scripts\activate
    
  • On macOS/Linux:

    source myenv/bin/activate
    

After activation, your terminal prompt will change, showing the name of the virtual environment (e.g., (myenv)).


3. Install Dependencies

With the virtual environment activated, you can now install packages using pip:

pip install package_name

For example:

pip install requests

To save installed packages to a requirements file:

pip freeze > requirements.txt

4. Deactivate the Virtual Environment

When you're done working in the virtual environment, deactivate it by running:

deactivate

This will return your terminal to the global Python environment.


5. Re-Activate Later

If you come back to the project later, re-activate the virtual environment using the activate command from step 2.


6. Install Dependencies from a Requirements File

To install dependencies listed in a requirements.txt file, use:

pip install -r requirements.txt

Using venv helps keep project dependencies organized and prevents conflicts between projects. If you have further questions or need help with specific configurations, let me know!

댓글

이 블로그의 인기 게시물

Fundamentals of English Grammar #1

The logs of the kubelet service

Firebase with Flutter

max_active_runs of Airflow

운영체제를 개발 방법

Auto-populate a calendar in an MUI (Material-UI) TextField component

Decimal (ASCII) value of each letter in the alphabet

"Do you happen to"

php 에서의 static 변수