Python for Windows Programming Environment

1. Purpose of the article

       This article is mainly to organize the basic settings of the Python scripting language development environment in the Windows environment, especially for installing multiple different versions of Python on the same system. Because of unfamiliarity, a lot of time is wasted in program creation, and I personally lacked summary before. , this is a summary of the information compiled by myself through a series of documents. Although the content is relatively basic, it is an accumulation process. It is once again clear that this article mainly solves the selection and switching and basic use of multiple Python versions in the Windows environment. There are many shortcomings. comminicate.

2. Python environment configuration

        In the Windows environment, installing a single version of Python is actually very simple. I won’t go into details here. I would like to organize it here. After installation, I will explain the installation of some basic libraries, environment variable configuration and some basic installation commands.

1. Environment variable configuration

        (1) What are environment variables?

        Environment variables are some parameters set in the operating system for the operating environment of the operating system. Simply put, they provide some default configurations for the operating system to guide the operating system to better execute startup applications. They are similar to the registry, but they are similar to the registry. There is a big difference. Environment variables are more about guiding the path of program running. For example, when you install an application on your computer, such as the QQ program, the shortcut will indicate the installation location of the QQ program, as shown in the figure:

         Therefore, when the user clicks on the icon, the operating system can accurately find the installation directory of the running program, but when you run it in cmd command line mode, it cannot find the installation program path because the operating system does not know where the program is installed. , there are functions of environment variables (it should have other functions, the knowledge is limited and only the ones we know are discussed). In response to the situation described above, when running the QQ program in the cmd command window to start the program, it first searches in the directory specified by the environment variable. In fact, the operating system also specifies some paths by default. If it is searched, it will run. Without searching, it cannot run. This This is why when we sometimes follow the development environment or part of the program, we will be prompted whether to add environment variables.

(2) Environmental variable classification

        Environment variables are divided into system variables and user variables. System variables are system-level variables. You need to use system variables. If the system variables change, any system user on the computer will be affected, and the user variables of different users will be affected. They are parallel and do not interfere with each other. The specific relationship is as shown in the figure:

Note: When the same environment variable is set in both system environment variables and user variables, the system defaults to limited use of the value set by the system environment variable.

 (3) How to set Windows environment variables

        In fact, there are many ways to set Windows environment variables. Here we only introduce some of them. The reason why recording is not commonly used, especially for some newcomers. In addition, I want to record more faster viewing and setting methods.

        (1) Windows operation interface mode

        Step 1: Open the run window on your computer (the opening method is win+R), and then enter sysdm.cpl

        

        Step 2: Select Advanced in the system properties interface, and then click Environment Variables:

 Step 3: Add and set environment variables according to your own needs

 Note: When there are multiple parameters in environment variables, they need to be separated by English semicolons, otherwise problems will occur;

(2) Set via command line

        1) View environment variables

                View the contents of specific variables: use the command echo %environment variable name%, such as PATH variables, etc.

                View system environment variables:

                        set: View all system environment variables;

                        set USER: View the environment variables of all users;

                        set %SUERPROFILE: View the environment variables of a specific user;

        2) Set environment variables

               set variable =

                Add a new variable after a variable:

                set PATH=%PATH%; new variable;

                Set system environment variables:

                        setx system environment variable "variable value" /M

                Set user environment variables

                        setx user environment variable "variable value"

        3) Delete environment variables

                setx environment variable ""

(3) Modify through environment variables

       Open the run window through win+r, enter regedit, and open the registry.

        System environment variable location:                HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SessionManager\Environment

        User environment variable location

         HKEY_CURRENT_USER\Environment

      The above provides three methods for modifying and viewing environment variables, which may seem a bit redundant to most people, but they will be helpful for further research on computers and for subsequent more in-depth study.

2. Multiple Python version configuration and switching

        Because Python installation is relatively simple, I won’t go into details here. Here I only talk about how to switch and use when installing multiple versions of Python for an environment. There are actually two situations for installing multiple versions in the same environment. The first situation is: installing different major versions of Python, namely Python2 and Python3; the second situation is the same major version with different minor versions, such as Python3.7 and P. Cherry red version 3.10, etc.

        Sometimes there are many versions of Python installed in the system. When writing a program or configuring a development environment or installation package, you don’t know which version to install, which leads to many problems. Let’s sort out this problem. The specific switching method is as follows:

        (1) Modify the Python name to distinguish it

        Modifying the Python name means creating a copy of the Python.exe program in the directory where the executable program is located for different versions of Python, and changing the name to the corresponding version, such as Python2.exe/Python3.exe. At the same time, you must also modify pip.exe Make corresponding modifications to the program. The commands for the installation package are as follows:

        python-name.exe -m pip install package name

        (2) Distinguish through Python launcher py.exe

        During the Python installation process, the Py.exe program will install the core to start py.exe. By passing parameters, you can identify and start the Python version installed on the system. You can view detailed commands through py -h:

py -2   //启动2版本的
py -3   //启动3版本的
py -X.X //启动x.x版本的python
py -2 -m pip install xxxxxx //在2版本下进行安装,同理其他版本

        (3) Directly modify environment variables

        The above two methods can only be used to manually switch the Python environment. Sometimes in the command line, we want to use which Python version is used by default or the Python version used by the system by default. In this case, we directly modify the environment variables. Either delete the environment variables or set the default environment variables in order, where the default Python installation path is placed in front of the environment variables, as shown in the figure:

        In the figure, Python310 is in front of Python311. Therefore, the system calls Python310 by default.

3. Basic library installation

        1.pip package installation       

         After Python is installed, the next step is to install the package. The most important basic package is pip, which can quickly install other packages. There are two ways to install the pip package, offline installation and online installation, as follows:

        (1) Offline installation

          Download pip from the pip official website: https://pypi.python.org/pypi/pip, unzip it, go to the directory, and run the following command:

        python setup.py install

        The packages installed by Python are all under the Python/scripts folder, so they need to be added to the environment variables.

        (2) Online installation

        The so-called online installation means that you can automatically download and install through the command line instead of downloading the installation package:

                py -m ensurepip --upgrade

        Depending on the version of this method, the ensurepip module may not exist, causing the installation to fail. Therefore, it is recommended to use the offline package method for installation.

        The basic usage of pip is as follows:

                pip --version: Check the version of pip. Please note that the pip installation package must be consistent with your target python version;

                python -m pip install --upgrade pip: self-version upgrade of pip;

                pip install installation package name: install the package with the specified package name;

                pip install package name == version number: install the specified version of the package;

                pip list: View installed packages;

                pip uninstall installed package name: uninstall the installed package;

                pip search package name: search for the specified package;

                pip show: displays package information;

                pip show -f package name: View information about the specified package;

                pip install -i https:// pypi .tuna.tsinghua.edu.cn/simple package name: Install the specified package name at the specified source address;

        2.virtualenv package

        This package is a virtual environment tool that can create an isolated Python environment so that packages or executable files installed in the virtual environment will not affect other virtual environments and packages and environments in the real system. It creates a directory containing all the executables and libraries required for that particular environment. Virtualenv uses the concept of a "virtual" or "sandbox" Python interpreter, where the shell creates an instance of the Python interpreter every time you run a command using virtualenv.

        Sometimes different libraries and packages need to be installed for different projects. What is embarrassing is that sometimes some packages and libraries are incompatible between versions. For this, virtual environment tools come in handy. The specific uses are as follows:

        Create a virtual environment:

        virtualenv virtual environment folder name: the python version in this environment is the default version in the system;

        virtualenv -p python installation path virtual environment folder name: specify the python version in the virtual environment;

        Open the virtual environment startup folder and execute:

        Enter the Scripts folder in the virtual environment

                activate: start the virtual environment;

                deactivate: Exit the virtual environment;

        In addition to the above virtualenv package, there is also a similar package virtualenvwrapper

This time I will write so much, which is relatively basic. On the one hand, the purpose of the article is to systematically organize and organize the information. At the same time, it is also to share and point out the shortcomings.

        

Guess you like

Origin blog.csdn.net/x_ox002/article/details/131342040