Prepare a Python virtual environment for your project with venv with VS Code

Introduction

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.

Said thing Building Python development environment with Visual Studio Code (from 0 to static analysis and code formatting with flake8)

Operation check environment

Windows 10 64bit Python 3.8.3 VSCode 1.45.1

Python virtual environment construction

Create directory for your project

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

Creating a virtual environment

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

PowerShell policy changes

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.

1. 1. Open PowerShell with administrator privileges

Find PowerShell from the search and open it with administrator privileges. Powershell.png

2. Execute the following command in PowerShell

Set-ExecutionPolicy RemoteSigned

Reflect Python virtual environment in VS Code

1. Open the project directory with VS Code

Click File ⇒ Open Folder from the VS Code top menu to open the created project directory.

2. Create a python file for confirmation

If there is no Python file in the project folder, the virtual environment will not be loaded, so create an arbitrary * .py file.

3. Confirmation of reflection of virtual environment

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. venv2.PNG

4. Check the terminal

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.

terminal2.PNG

Installation of required modules

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 pipfreeze.png

That is all for building a virtual environment. Thank you for your hard work!

Referenced articles

Notes on using venv with VS Code of Windows 10

Recommended Posts

Prepare a Python virtual environment for your project with venv with VS Code
Python (Windows 10) Virtual Environment / Package with VS Code
Make your Python environment "easy" with VS Code
Build a python execution environment with VS Code
I was addicted to creating a Python venv environment with VS Code
Steps to create a Python virtual environment with VS Code on Windows
Create a virtual environment with Python!
Building a virtual environment with Python 3
Build a Python environment with WSL + Pyenv + Jupyter + VS Code
Let's create a virtual environment for Python
[Python] Create a virtual environment with Anaconda
[Mac] Building a virtual environment for Python
Build a python virtual environment with pyenv
Create a Python environment for professionals in VS Code on Windows
Create a simple Python development environment with VS Code and Docker
How to create a Python virtual environment (venv)
Create a virtual environment with conda in Python
Build a virtual environment with pyenv and venv
[Venv] Create a python virtual environment on Ubuntu
Commands for creating a python3 environment with virtualenv
Work in a virtual environment with Python virtualenv.
Procedure for creating a Python quarantine environment (venv environment)
Use Python in Anaconda environment with VS Code
Virtual environment with Python 3.6
Python: Creating a virtual environment (venv), starting and stopping
Build a python virtual environment with virtualenv and virtualenvwrapper
Build a python environment for each directory with pyenv-virtualenv
VS Code settings for developing in Python with completion
Build a Python virtual environment using venv (Django + MySQL ①)
Building and enabling a python virtual environment, etc. (venv)
Build a python virtual environment with virtualenv and virtualenvwrapper
Build a Django environment for Win10 (with virtual space)
Python with VS Code (Windows 10)
Debug Python with VS Code
Prepare python3 environment with Docker
Building a Python virtual environment
venv: Python virtual environment management
python standard virtual environment venv
Building a Python virtual environment
Environment construction for those who want to study python easily with VS Code (for Mac)
Set up a Python development environment with Visual Studio Code
Enable the virtualenv Python virtual environment for Visual Studio Code
Prepare Python development environment with Mac + Windows + VisualStudio Code (Windows version)
[Python] Building a virtual python environment for the pyramid tutorial (summary)
Prepare Python development environment with Mac + Windows + VisualStudio Code (Mac version)
Create a virtual environment for python on mac [Very easy]
Build a python environment on CentOS 7.7 for your home server
[Python] Create a screen for HTTP status code 403/404/500 with Django
[No venv required] The strongest Python development environment created with Remote Containers [VS Code / Docker]
Build python virtual environment with virtualenv
Prepare your first Python development environment
Prepare Python development environment with Atom
Install python with mac vs code
Create a Python (Django) learning environment with Docker so that you can debug with VS Code (almost your own procedure memo)
Create a Python execution environment for Windows with VScode + Remote WSL
Building a Windows 7 environment for getting started with machine learning with Python
Build a Python environment on your Mac with Anaconda and PyCharm
Try using virtualenv, which can build a virtual environment for Python
Try to create a python environment with Visual Studio Code & WSL
Prepare a distributed load test environment with the Python load test tool Locust
Debug settings in virtual environment when using Pipenv with VS Code