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!

댓글

이 블로그의 인기 게시물

Using the MinIO API via curl

How to split a list into chunks of 100 items in JavaScript, 자바스크립트 리스트 쪼개기

HTML Inline divisions at one row by Tailwind

Boilerplate for typescript server programing

가속도 & 속도

Gradle multi-module project

How to checkout branch of remote git, 깃 리모트 브랜치 체크아웃

CDPEvents in puppeteer

Sparse encoder

Reactjs datetime range picker