Frequently Asked Questions for Python Beginners 1


foreword

Today I saw a related article about the problems that a Python beginner may encounter. It is well written, please reprint it.
reference link


1. How many Pythons can be installed on one computer?

There are some Python beginners who started with Anaconda when learning online tutorials. At this time, the first Python environment in their computer is the Python in Anaconda. In this case, can I download and install other versions of Python directly from the Python official website?
We know that Python2 and Python3 can coexist. So can Python 3.5 and Python 3.6 coexist? Can Python 3.6.1 and Python 3.6.2 coexist?
What I want to tell you is that not only can you install Python from the official website after you have Anaconda, but you can actually install as many Pythons as you want in your computer. Even if you want to install 10 Python3.7.1 on the computer.

2. What is the essence of installing Python

1. How to call different Python interpreters?

When you install Python, you essentially install a folder on your computer. There is a python.exe and various subfolders in this folder.
So, you can install Python 3.7.1 to C:\python371, put Python 3.7.2 Install to the two folders C:\python372 When
you use C:\python371\python xxx.py, you use Python 3.7.1 to run this file
When you use C:\python372\python When xxx.py is used, Python 3.7.2 is used to run this file

2. Which Python is called in the CMD command line?

When we directly enter python xxx.py in CMD, which Python does it use? It's actually not about Python at all. This is determined by the system environment variables.
No matter which system, when you execute python xxx.py, it will first check whether python is a system command, and if it is not, it will look for an executable file named python.exe under the current folder (or python.bat), and found nothing. At this time, it will search for folders one by one according to the folders recorded in the environment variables.
For example, the environment variable is: c:\windows\system32;c:\program files;c:\python371;c:\python372. Then at this time, CMD will first look for python.exe under the C:\Windows\System32 folder, and find that it cannot be found, so it goes to the c:\ProgramFiles folder to find it, and finds that it cannot be found, and then checks C:\ The python371 folder was found, so I used Python 3.7.1 to run the file.
What if you want to use Python 3.7.2 to run by default when running python xxx.py in CMD? It's very simple, just change the environment variable to
c:\windows\system32;c:\program files;c:\python372;c:\python371.
Therefore, you can install 100 Pythons of the same version or different versions on your computer. Whichever you want to use, you can start it directly through the absolute path, or modify the environment variable to set the version of Python you want to use to the maximum Front.

3. Where are the three-party libraries installed in Python?

As for the third-party libraries installed in Python, it is also very simple. If you open the Python installation folder, you can find that there is a site-packages folder (the conda virtual environment is in envs/virtual environment/lib/site-packages), when You use the pip corresponding to a certain version of Python (at this point you should write: c:\python371\python-m pip install xxx) After installing a third-party library, the third-party library will be placed in this folder in. When the program you run in Python needs to use a third-party library, it will look for it in its own site-packages folder.
Therefore, since different versions of Python have their own site-packages folders, the third-party libraries installed by them do not interfere with each other.
Some third-party libraries you install generate an executable file. For example, after you install Scrapy, you will find that you can execute scrapy commands in CMD. In fact, the essence is that pip copies a scrapy executable file to the scripts folder (bin folder for macOS or Linux) in the folder corresponding to the Python version. When you execute the scrapy command, CMD will go to the corresponding folders in the environment variable to find the corresponding scrapy.exe file, and run it when it finds it.
Therefore, it may happen that you installed Scrapy in the Python 3.7.2 environment, but when you run it in CMD, it prompts that the scrapy command cannot be found. In this case, it may be because the path of other Python is set in your environment variable, and the path of Python 3.7.2 is not set.

Python's virtualenv is essentially the same principle. When there is only one Python in your computer, you can create another environment through virtualenv, which looks like a copy of the Python in the system environment. So when you use Python in the virtual environment, the installed third-party libraries are all installed in the site-packages folder of the virtual environment, which will not affect the Python in the system environment.
Of course, when virutalenv creates a virtual environment, it does not really copy all the files. Instead, a soft link is created. What runs through this soft link in the virtual environment is essentially the Python of the system environment, but since this virtual environment also has a site-packages folder, it will use the site-packages of the virtual environment. This explains why when you create a virtual environment and delete the Python in the system environment, you will find that the Python in the virtual environment cannot run.


Summarize

The above is what I want to talk about today. This article only briefly introduces the problems related to python installation location.

Guess you like

Origin blog.csdn.net/goodlmoney/article/details/126770718
Recommended