It's time to summarize a wave of Python environment construction problems

Preface

The text and pictures in this article are from the Internet and are for learning and communication purposes only. They do not have any commercial use. If you have any questions, please contact us for processing.

PS: If you need Python learning materials, you can click on the link below to get it yourself

Python free learning materials and group communication answers Click to join


1. Python use positioning and IDE

The current role of the individual is still based on data analysis + algorithm, taking into account engineering realization. So my favorite IDE combination is JupyterLab+VSCode+Pycharm. Of course, JupyterLab is the main environment for data exploration and analysis, and VSCode is used for viewing source code and document editing. Once the algorithm flow is determined and the formal Python code file needs to be output, then Will use Pycharm. This order is not only the use frequency from large to small, but also the IDE size and portability from simple to complex.


Note: Pycharm distinguishes two versions of Pro and Community. The former is more functional and powerful, but requires a fee to use, while the latter is a free community version. I personally think that the Community version is good enough. In addition, the Anaconda environment recommended by many data practitioners, in fact, personally disagrees. It has no substantial advantages other than pre-installed Python and some third-party libraries.

 

2.Python installation

There is not much to say about the installation of Python, just download the corresponding version from the official website. The latest version is 3.9.1. According to the latest plan of Python, there will be a major version iteration every year, which means that 3.10 will be launched in 2021. In 2022 it will be 3.11. But in fact, my current favorite version is still 3.7 or 3.8. Since most third-party libraries need to be installed separately, Python is very small, only 20+M. A very noteworthy problem here is that Python is installed by default by different users. At this time, the installation path will be automatically located under the appdata directory of the C drive user, and when the user is not an administrator, the installation path cannot be changed at will. The effective solution at this time is to check the "install for all users" option, and the corresponding installation directory will be located in the default program files directory of most software.

 

 

After Python is installed, if some third-party libraries are not installed, then Python is actually very weak at this time, so naturally the second problem is how to efficiently install third-party libraries. In this regard, a brief summary is as follows:

1. pip tool installation problem

Under normal circumstances, installing Python will install the pip tool by default and add it to the system environment variables, so that third-party libraries can be managed through the pip tool. Check the following during installation:

 

 

If for various reasons, when the system cannot use the pip command, the system prompts that pip is not an internal or external command, it is only because the pip application is not added to the environment variable.

 


The way to solve this problem is very simple, find the pip.exe execution path in the Python installation directory, and then add it to the environment variable.

2. pip install third-party library problem

pip is a tool for managing third-party libraries. Common operations include install, uninstall, and upgrade. Among them, install can distinguish between online download and installation and offline installation of wheel files.

  • Online installation. Online installation is very simple, but limited to the download speed, generally you need to configure the pip domestic download source. You only need to create a pip folder in the user directory, and then create a pip.ini file in the folder, and write the following 2 lines of code (here Take changing the source of Tsinghua University as an example)
[global] 

index-url = https://pypi.tuna.tsinghua.edu.cn/simple
  • Install offline. Online installation is very convenient, but for some confidential or offline environments where the Internet is inconvenient, the more effective method is offline installation. At this time, you can first prepare the package to be installed on a networked machine, and then execute the following 3 commands. , The first two commands are used to prepare offline installation files on the networked machine, and the third command is used to perform offline installation on the target machine.
1.导出已安装pip列表:pip freeze >requirements.txt
2.根据列表下载安装文件 pip download -d packages -r requirements.txt
3.根据文件和列表离线安装 pip install --no-index --find-links=packages -r requirements.txt

3.Jupyter Lab configuration

Under normal circumstances, when installing third-party Python libraries, the Jupyterlab library will definitely be installed together. By default, after the installation is complete, you can directly type jupyter lab under the cmd command to start the browser and build the ipython operating environment. There are still 3 issues that need to be paid attention to here:
1. The space issue before Jupyter lab. This is a very small detail. When pip install, jupyterlab is written together to indicate a package name, and when you type the jupyter command to start the browser, you need to write it separately, where jupyter stands for command (the corresponding behind is jupyter.exe ), lab stands for parameter. In addition to lab as a parameter, of course, another optional parameter is notebook.

2. By default, as long as the installation of jupyter lab is normal, then typing jupyter lab in cmd can directly start the browser to build the environment, but there are also abnormal situations, just like the pip command is not recognized, when the jupyter command is not recognized, Still have to think that the path is not added to the environment variable. So the solution is the same as the previous pip command.


3. Modify the default working directory of jupyter lab. Generally speaking, after jupyter lab is started, the default working directory is the installation path, but this may be a scenario that most people do not want, so naturally it needs to be changed. The way to change is to execute the following command in cmd:

 

jupyter lab --generate-config

 

Then open the newly generated configuration file, find the c.ServerApp.notebook_dir parameter, and modify it to the target path address. For example, modify it to the root directory of Disk D in the figure.

 

4.VSCode configuration

VSCode is a code editor launched by Microsoft. More essentially, it is a text editor. Similar applications include Sublime, Notepad++, etc. Although the functions are similar, VSCode has a strong endorsement from Microsoft and more and more plug-ins. The frame bonus is now gradually showing an increasingly strong competitiveness.

Individuals are generally used to using it as a software to view the code. It is very fast to use the ctrl+B shortcut to find references. In addition, it also integrates Git and debug functions. Of course, if you want to make it really easy to use, you actually need to install some plug-ins. In the case of networking, just click on the extended menu on the left to easily find and install the specified extension applications, such as the Chinese package (Chinese), code beautification tool (beautify) and Python language related, these are almost essential plug-ins.


Of course, the need to install plug-ins offline should also be considered, and VSCode naturally takes this scenario into consideration. Go to the following website (VSCode plug-in market: https://marketplace.visualstudio.com/ ), enter the plug-in name, click to go to the details page, and click to download the extension on the right.

 

Then, similar to using the pip tool to install Python third-party libraries, you can directly use the following commands to install the VScode extension offline. Where xxx represents the previously prepared extension, and the premise that the code command can be recognized is that the path of vscode.exe is required to be added to the environment variable.

code --install-extension xxx

Of course, there is also the configuration of Pycharm. Compared with Python, Jupyter and VSCode, the installation of Pycharm is actually highly integrated and does not require much configuration. But it is worth studying in-depth that Pycharm's virtual development environment, which is also a great weapon of Pycharm, will not be expanded here.

Guess you like

Origin blog.csdn.net/pythonxuexi123/article/details/113108800