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.
Windows 10 Pro
We will proceed according to the following flow.
First, install Python. Download the base Python from the following site and install it on your PC.
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)
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
** ■ 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.
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.
** ■ 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.
pip list
.** ■ 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! ** **
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