Python programming basics-programming environment preparation
Written in front of this series of blog articles aimed at non-computer majors, especially humanities and social sciences, management economics and other related majors or personnel learning to pave the way for practical data analysis and visualization, machine learning, etc., selected blogs or books for reference, concise and practical The premise is to focus on task orientation and practical exercises .
Anaconda installation and use
Introduction
- Open source package management system and environment management system, including multi-language package installation, operation, update, deletion, and most importantly, it can solve the problem of package dependency
- Support languages include Python, R, Ruby, Lua, Scala, Java, JavaScript, C/C++, FORTRAN
- Supports running on Windows, macOS and Linux
- Conda can build different environments, and at the same time save, load and switch the environment
- Conda package and environment manager are included in all versions of Anaconda and Miniconda
Download and install (this article only uses Win10 as an example)
Reference: Anaconda introduction, installation and usage tutorial .
-
Go to the official download page to download. At present, the official website only provides Python version 3.8. According to the Win10 operating system, click "64-Bit Graphical Installer" or "32-Bit Graphical Installer" to download.
This tutorial takes Anaconda3-5.2.0-Windows-x86_64 version (Python3.6) as an example. Provide Baidu network disk download address Extraction code: gr81 -
After the download is complete, double-click the downloaded file to start the installer.
-
Select "Next".
-
Read the terms of the license agreement, then check "I Agree" and proceed to the next step.
-
Unless you are installing as an administrator for all users, just check "Just Me" and click "Next".
-
In the "Choose Install Location" interface, select the target path to install Anaconda, and then click "Next".
Note: The target path cannot contain spaces or Chinese.
- In the "Advanced Installation Options" in to check the "Add Anaconda to my PATH environment variable ." ( " Anaconda Add to my environment variable.").
Note: It is recommended not to check some information, because if checked, it will affect the use of other programs, and the author has not found it yet.
- Click "Install" to start the installation. If you want to view the installation details, you can click "Show Details".
- Click "Next".
- Entering the "Thanks for installing Anaconda!" interface means that the installation is successful, click "Finish" to complete the installation.
Note: If you don't want to know about "Anaconda Cloud" and "Anaconda Support", you can uncheck "Learn more about Anaconda Cloud" and "Learn more about Anaconda Support".
- Verify the installation result. You can choose any of the following methods:
- "Start → Anaconda3 (64-bit) → Anaconda Navigator", if you can successfully start Anaconda Navigator, the installation is successful.
- "Start → Anaconda3 (64-bit) → right-click Anaconda Prompt → run as administrator", enter in Anaconda Prompt
conda list
, you can view the installed package name and version number. If the result can be displayed normally, the installation is successful.
Basic use of Pip and mirror source settings
Introduction
- pip is a modern, general-purpose Python package management tool. Provides the functions of searching, downloading, installing, and uninstalling Python packages
Pip mirror source settings (this article only uses Win10 as an example)
By default, pip uses the official Python source to set up overseas. The download speed is slow or the connection timeout, so you need to use the domestic Python mirror source.
Reference: How to switch pip mirror source (installation source) in Python .
Common domestic mirror sources
http://pypi.douban.com/simple/ 豆瓣
http://mirrors.aliyun.com/pypi/simple/ 阿里
http://pypi.hustunique.com/simple/ 华中理工大学
http://pypi.sdutlinux.org/simple/ 山东理工大学
http://pypi.mirrors.ustc.edu.cn/simple/ 中国科学技术大学
https://pypi.tuna.tsinghua.edu.cn/simple /清华大学
-
Enter "%APPDATA%" in the windows file manager, as shown below:
-
"Enter" will locate a new directory ("C:\Users\Administrator\AppData\Roaming\pip\pip.ini"), Administrator is your user name, create a pip folder in this directory, and then Create a new pip.ini file in the pip folder, as shown below:
-
Enter the following content in the newly created pip.ini file, and then save it.
Note: The content of "index-url" is the path of the mirror source, which can be replaced
[global]
timeout = 6000
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
trusted-host = pypi.tuna.tsinghua.edu.cn
As shown below (screenshot of replacing Tsinghua source):
Notepad++ 7.8.9 official Chinese version download link
Basic use of Pip
Reference: Basic use of pip and offline installation of Python third-party libraries .
pip command and its parameters
Enter the command in the command line window to pip --help
view the parameters and usage of the pip command, as shown in the following figure:
# pip --help
Usage:
pip <command> [options]
Commands:
install Install packages. 安装包
download Download packages. 下载包
uninstall Uninstall packages. 卸载包
freeze Output installed packages in requirements format. 按着一定格式输出已安装包列表
list List installed packages. 列出已安装包
show Show information about installed packages. 显示包详细信息
check Verify installed packages have compatible dependencies.检查包的依赖关系是否完整
config Manage local and global configuration.管理配置
search Search PyPI for packages.搜索包
wheel Build wheels from your requirements.
hash Compute hashes of package archives.计算包的hash值
completion A helper command used for command completion.
help Show help for commands.
General Options:
-h, --help Show help.
--isolated Run pip in an isolated mode, ignoring environment variables and user configuration.
-v, --verbose Give more output. Option is additive, and can be used up to 3 times.
-V, --version Show version and exit.
-q, --quiet Give less output. Option is additive, and can be used up to 3 times (corresponding to WARNING, ERROR, and CRITICAL logging levels).
--log <path> Path to a verbose appending log.
--proxy <proxy> Specify a proxy in the form [user:passwd@]proxy.server:port.
--retries <retries> Maximum number of retries each connection should attempt (default 5 times).
--timeout <sec> Set the socket timeout (default 15 seconds).
--exists-action <action> Default action when a path already exists: (s)witch, (i)gnore, (w)ipe, (b)ackup, (a)bort).
--trusted-host <hostname> Mark this host as trusted, even though it does not have valid or any HTTPS.
--cert <path> Path to alternate CA bundle.
--client-cert <path> Path to SSL client certificate, a single file containing the private key and the certificate in PEM format.
--cache-dir <dir> Store the cache data in <dir>.
--no-cache-dir Disable the cache.
--disable-pip-version-check
Don't periodically check PyPI to determine whether a new version of pip is available for download. Implied with --no-index.
--no-color Suppress colored output
Common pip commands
- Install the module
pip install <包名>
- Uninstall module
pip uninstall <包名>
- Upgrade module
pip install -U <包名>
- View installed packages and their versions
pip freeze
- View upgradeable packages
pip list -o
Note: When the following statement appears, you should upgrade pip as prompted.
You are using pip version 10.0.1, however version 20.2.3 is available.
You should consider upgrading via the'pip install --upgrade pip' command.
This sentence means "You are using pip version 10.0.1, but you can Use version 20.2.3. You should consider upgrading via the "pip install –upgrade pip" command"
- Upgrade pip
python -m pip install --upgrade pip
Introduction, operation and use of Jupyter Notebook
Reference: Jupyter Notebook introduction, installation and usage tutorial .
Introduction
- Jupyter Notebook is a web-based application for interactive computing. It can be applied to the whole process of calculation: development, document writing, running code and displaying results.
Run Jupyter Notebook
Enter the following command in the terminal:
jupyter notebook
After executing the command, a series of notebook server information will be displayed in the terminal, and the browser will automatically start Jupyter Notebook.
The terminal display content during startup is as follows:
[I 18:47:37.542 NotebookApp] Writing notebook server cookie secret to C:\Users\MXL\AppData\Roaming\jupyter\runtime\notebook_cookie_secret
[I 18:47:39.002 NotebookApp] JupyterLab beta preview extension loaded from C:\Users\MXL\Anaconda3\lib\site-packages\jupyterlab
[I 18:47:39.003 NotebookApp] JupyterLab application directory is C:\Users\MXL\Anaconda3\share\jupyter\lab
[I 18:47:39.895 NotebookApp] Serving notebooks from local directory: C:\Users\MXL
[I 18:47:39.895 NotebookApp] 0 active kernels
[I 18:47:39.897 NotebookApp] The Jupyter Notebook is running at:
[I 18:47:39.899 NotebookApp] http://localhost:8888/?
Note: After all operations in Jupyter Notebook, please keep the terminal not closed, because once the terminal is closed, the link to the local server will be disconnected, and you will not be able to perform other operations in Jupyter Notebook.
The browser address bar by default will be displayed: http://localhost:8888
. Among them, "localhost" refers to this machine, and "8888" is the port number.
Main page (introduction in the next section)
After executing the startup command, the browser will enter the main page of Notebook, as shown in the figure below.
Set Jupyter Notebook file storage location
If you don't want to save all documents written in Jupyter Notebook in the future directly in your home directory, then you need to modify the file storage path of Jupyter Notebook.
- Create folder/directory
Windows users create a new folder in the disk where you want to store Jupyter Notebook files and name the folder; double-click to enter the folder, and then copy the path in the address bar.
- Configuration file path
Enter the following command in the terminal (generate configuration file):
jupyter notebook --generate-config
Note: If the configuration file already exists or has been modified, the query "Overwrite /Users/XXX/.jupyter/jupyter_notebook_config.py with default config? [y/N]" will appear after using this command, that is, "overwrite this with the default configuration file Is the file under the path?", if you press "y", the overwriting is completed, then the previous modifications will be invalid; if it is just to query the path, then you must enter "N".
- Modify the configuration file
(A) Open the configuration file
Windows system users can use the document editing tool Notepad++ to open the "jupyter_notebook_config.py" file and edit it.
(B) Search for keywords After
entering the configuration file, Ctrl+F
search for the keyword "c.NotebookApp.notebook_dir".
(C) Edit the configuration file
(D) Uncomment
Delete the pound sign (#) at the beginning of the line. Because the configuration file is a Python executable file, in Python, the pound sign (#) indicates a comment, that is, the command line will not be executed during the compilation process, so in order to make the modification effective, you need to delete the pound sign (#).
(E) Save the configuration file
(F) Verification
Enter the command jupyter notebook in the terminal to open Jupyter Notebook. At this time, you will see a refreshing interface, congratulations!
Note: All the above commands are entered in English half-width format. If there is an error, please strictly check these two conditions, English and half-width.