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

vsftpd default directory

[Ubuntu] *.deb 파일 설치 방법

Offset out of range error in Kafka, 카프카 트러블슈팅

리눅스 (cron - 주기적 작업실행 데몬)

리눅스 (하드링크&소프트링크)

CDPEvents in puppeteer