Manage multiple execution environments using Python venv

Introduction

If you are using Python, you may see "I want to manage the execution environment for each project" and "I want to use the library as a trial". In such a case, it is convenient to build a virtual environment for each execution environment, so this article summarizes the method.

Installation environment

Windows 10 Pro

Building a virtual environment

We will proceed according to the following flow.

    1. Install Python (not required if you already have it installed)
  1. Building a virtual environment using venv
    1. Install a specific library in the built virtual environment

1. 1. Install Python (not required if you already have it installed)

First, install Python. Download the base Python from the following site and install it on your PC.

Official python download page

If you have trouble installing it, you should refer to the following site. Download and install Python 3.7.3

Also, if you want to start over from the installation once, this site will be helpful. How to uninstall Python completely (Windows)

2. Building a virtual environment

There are actually some tools to build a virtual environment, so this time we will build it using ** "venv" **! (If you are concerned about the advantages and disadvantages of each tool, the following site may be helpful!) Python environment management tool is good or bad

Now let's create a virtual environment!

** ■ Create a folder for virtual environment ** This is just preparing a place to put the virtual environment, so you can create a folder in any place you like.

** ■ Start the command prompt and move to the folder created above. Then execute the following command to create a virtual environment. ** ** python -m venv for_scraping The [for_scraping] part can be any name you like. The project name may be safe.

3. 3. Install a specific library in the built virtual environment

Since the built virtual environment is still in the same state as the local environment, try putting the scraping library only in the virtual environment.

** ■ Activate the virtual environment * Note that if you forget this, it will be installed in the local environment! ** ** . \ For_scraping \ Scripts \ acknowledge (For Linux / Mac, bin instead of Scripts) The virtual environment is active if it is in the following state. Qiita-no002_img02.jpg

** ■ Install scraping library (Beautiful soup) ** pip install beautifulsoup4

** ■ Exit the virtual environment and confirm that it is not installed in the local environment ** Exit the virtual environment: detectivate Execute the following test.py to confirm that it is not installed. (Just create [test.py] in advance with an editor and execute Python test.py.)

test.py


from bs4 import BeautifulSoup

If "No module named" is displayed, confirmation is complete. Qiita-no002_img03.jpg

** ■ Activate the virtual environment again and check ** Activate virtual environment: . \ For_scraping \ Scripts \ acrivate Run test.py. (If no error is displayed, the execution is complete!)

** Now you can confirm that it can be installed only in the virtual environment! ** **

Summary

When I want to try out a new library, or when I want to separate the execution environment for each project, I feel that it is quite convenient. How to set the virtual environment as the execution environment of Juypter Notebook is summarized in ** here **!

Recommended Posts

Manage multiple execution environments using Python venv
venv (Python)
Python development environment for macOS using venv 2016
Periodic execution processing when using tkinter [Python3]
Using venv in Windows + Docker environment [Python]
Install multiple versions of Polyphony using venv
Parallel task execution using concurrent.futures in Python
Manage multiple Python versions with update-alternatives (Ubuntu)
Manage Python multiple version environment with Pythonz, virtualenv
Start using Python
Building multiple Python environments on the same system
Manage multiple context managers together with Python contextlib.ExitStack
Scraping using Python
Build a Python virtual environment using venv (Django + MySQL ①)
How to retrieve multiple arrays using slice in python.
Operate Redmine using Python Redmine
Function execution time (Python)
Fibonacci sequence using Python
[Python] Create multiple directories
Data analysis using Python 0
Data cleaning using Python
Using Python #external packages
Output python execution time
WiringPi-SPI communication using Python
Age calculation using python
Search Twitter using Python
Name identification using python
Notes using Python subprocesses
Python3, venv and Ansible
Try using Tweepy [Python2.7]
Python Basic Course (3 Python Execution)
When you want to use multiple versions of the same Python library (virtual environment using venv)