In the above, we carried out environment construction and static analysis, When developing multiple projects in the same environment, modules to be used and modules of different versions are used, so Prepare a Python virtual environment for each project.
Windows 10 64bit Python 3.8.3 VSCode 1.45.1
Open New Terminal from Terminal in the top menu of VS Code. (Powershell or Cmd is OK) Create a project directory in any directory.
PS C:\work> mkdir TestProject
PS C:\work> cd TestProject
Execute the following command to build a virtual environment.
PS C:\work\TestProject> python -m venv project_env
After building the virtual environment, the following files are created.
PS C:\work\TestProject> cd project_env
PS C:\work\TestProject\project_env> ls
directory: C:\work\TestProject\project_env
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2020/06/02 15:00 Include
d----- 2020/06/02 15:00 Lib
d----- 2020/06/02 15:00 Scripts
-a---- 2020/06/02 15:00 126 pyvenv.cfg
To activate the virtual environment It seems that it is necessary to execute PowerShell Script with that function, but it seems that it can not be executed by default in Windows 10.
Therefore, change the PowerShell policy.
Find PowerShell from the search and open it with administrator privileges.
Set-ExecutionPolicy RemoteSigned
Click File ⇒ Open Folder from the VS Code top menu to open the created project directory.
If there is no Python file in the project folder, the virtual environment will not be loaded, so create an arbitrary * .py file.
If the virtual environment can be loaded normally, the name of the loaded virtual environment will be displayed next to the Python version information at the bottom left as shown below.
If you open the terminal with the virtual environment reflected, it will open with the virtual environment activated as shown below. Since it is a brand new environment, even if you display the installation package list with pip freeze, it will be empty.
In the virtual environment, the static analysis tool set in the above will not be installed, so install it in the virtual environment as well.
pip install flake8 autopep8
List of packages after installation
That is all for building a virtual environment. Thank you for your hard work!
Notes on using venv with VS Code of Windows 10
Recommended Posts