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!

댓글

이 블로그의 인기 게시물

Spring JPA에서 데이터베이스 연결 풀의 기본 값

Avro + Grpc in python

dagrun_timeout of Airflow