(windows) python installation tutorial

Table of contents

1. Download

2. Installation

3. Virtual environment

1. Install virtualenvwrapper-win and virtualenv third-party libraries

2. Configure environment variables for the virtual environment

3. Create a new virtual environment

4. Activate the virtual environment

5. Exit the virtual environment

6. Delete the virtual environment

7. Configure virtual environment in vscode


1. Download

Download and install python (check the official website)

2. Installation

After the download is complete, double-click to open the installer exe file you just downloaded, and the installation wizard for the Python environment will open.

When executing the installation wizard, remember to check the "Add Python 3.x to PATH" option. This option will help us add the Python interpreter to the PATH environment variable (it doesn't matter if you don't understand it, just do it).

After the installation is complete, you can press win+R (win is the key on the start menu) to open the Windows "Command Line Prompt" tool and enter python --version or python -V to check whether the installation is successful. The command line prompt can be in " Enter cmd in Run" to open it or find it in the attachment of the "Start Menu".

If you see the version number corresponding to the Python interpreter (for example: Python 3.9.12), your installation has been successful.

3. Virtual environment

1. Install virtualenvwrapper-win and virtualenv third-party libraries

Option 1 (not recommended): Virtualenv can be used for the virtual environment, but this needs to be installed.

pip install virtualenv

Option 2 (recommended): Python3 comes with venv and can directly replace Virtualenv.

The venv module provides support for creating lightweight "virtual environments" that provide isolation support from system python. Each virtual environment has its own Python binary (allowing for different Python versions of the authoring environment), and can have its own independent set of Python packages.

It should be noted that the environment created using the "venv" command in Python 3.3 does not include "pip", and you need to install it manually. But there is no problem as long as the version is above 3.3.

 Python's third-party library virtualenv can help us create a virtual environment. The installation command is as follows:

pip install virtualenv -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install virtualenvwrapper-win -i https://pypi.tuna.tsinghua.edu.cn/simple

PS: If you use the command in virtualenv to create a virtual environment, an environment will also be created in the user directory of the C drive by default, which will occupy the C drive memory. Therefore, we give priority to virtualenvwrapper-win, a way to create a virtual environment.

2. Configure environment variables for the virtual environment

Change name setting:WORKON_HOME, CautionChange name constant large copy< a i=4>;

The variable value is the directory path where you want to create an environment variable. It is recommended to create a new one on the D drive or E drive as a virtual environment. Directory, so there is more space.

The variable value I wrote when configuring the environment variable is: D:\my_ruanjian\python_envs, so when I create a new virtual environment, it will be stored in D:\my_ruanjian\python_envs by default.

3. Create a new virtual environment

For example: I want to create a virtual environment named my_env. I enter mkvirtualenv my_env in cmd (< a i=3>You can enter it in any directory of cmd):

mkvirtualenv hj

After the virtual environment my_env is created, it will automatically jump to the my_env directory. There is (my_env) in front of the path:

Moreover, the system will configure the Python environment installed in the computer for the newly created virtual environment by default (including only some execution commands, no related third-party modules). Of course, we can also reinstall Python in the new virtual environment my_env.

4. Activate the virtual environment

To activate a virtual environment, run activate in Scripts in the virtual environment directory.

cd D:\my_ruanjian\python_envs\hj\
.\Scripts\activate

After activation, we can add class libraries and use them in the same way as using the global environment.

5. Exit the virtual environment

Use the deactivate command to exit the virtual environment directly.

deactivate

6. Delete the virtual environment

Directly delete the folder where the virtual environment is located.

7. Configure virtual environment in vscode

Ctrl+shift+P or F1 to open the command panel, then select the python interpreter option, click to select a new interpreter, and add a new interpreter.

After adding the virtual environment, open the code, and the interpreter information in the lower right corner will display the interpreter in the selected virtual environment.

Guess you like

Origin blog.csdn.net/qq_45956730/article/details/134944559