Python setup details
Python environment variable configuration under Windows
By default, after installing python under windows, the system will not automatically add the corresponding environment variables. At this time, you cannot use python commands directly on the command line.
1. First, you need to register the python environment variable in the system: assuming that the installation path of python is c:\python2.6, modify my computer->Properties->Advanced ->Environment variables->PATH in system variables is:
(In order to run Python commands in command line mode, you need to append the directory where python.exe is located to the PATH environment variable.)
PATH=PATH;c:\python26
After the above environment variables are successfully set, you can use the python command directly on the command line. Or execute "python *.py" to run the python script.
2. At this point, you can still only run the python script through "python *.py". If you want to run *.py directly, you only need to modify another environment variable PATHEXT:
PATHEXT=PATHEXT;.PY;.PYM
3. In addition, in the process of using python, you may need to check a certain Help documentation for the command, such as using help('print') to view instructions for using the print command. The default installed python cannot view the help document, and simple configuration is required:
In the python installation directory, find python25.chm, use
hh -decompile .python26.chm
Decompile it, and then add the directory where it is located to the PATH environment variable mentioned above.
4. How to enable the Python interpreter to directly import third-party modules outside the default installation path?
In order to import third-party modules (such as modules written by yourself) other than the default installation path, you need to create a new PYTHONPATH environment variable whose value is the directory where this module is located.
Python Set the system default encoding
When python is installed, the default encoding is ascii. When non-ascii encoding appears in the program, python processing often reports the error UnicodeDecodeError: 'ascii' codec can't decode byte 0x?? in position 1: ordinal not in range(128), python cannot handle non-ascii encoding. At this time, you need to set the default encoding of python yourself, which is generally set to the utf8 encoding format.
To query the system default encoding, you can enter the following command in the interpreter:
>>>sys.getdefaultencoding()
Use when setting the default encoding:
- ##
>>>sys.setdefaultencoding('utf8')
The error AttributeError: 'module' object has no attribute 'setdefaultencoding' may be reported. Execute reload(sys) and the above command will pass smoothly.
At this time, when you execute sys.getdefaultencoding(), you will find that the encoding has been set to utf8, but the encoding modified in the interpreter can only be guaranteed to be valid for that time. After restarting the interpreter, you will find , the encoding has been reset to the default ascii, so is there any way to modify the default encoding of the program or system at once?
There are 2 ways to set the default encoding of python:
A solution is to add the following code to the program:
import sys reload(sys) sys.setdefaultencoding('utf8')
Another solution is to create a new one in python’s Lib\site-packages folder A sitecustomize.py, the content is:
# encoding=utf8 import sys reload(sys) sys.setdefaultencoding('utf8')
At this time, restart the python interpreter, execute sys.getdefaultencoding(), and find that the encoding has been set to utf8. After multiple restarts, the effect is the same. This is because the system calls this file by itself when python is started to set the system's Default encoding, without the need to manually add the solution code every time, is a once-and-for-all solution.
Another solution is to force the encoding to utf8 in all places involving encoding in the program, that is, add the code encode("utf8"). This method is not recommended. , because once one less place is written, it will lead to a large number of error reports. I once encountered this situation. After the error log was compressed, it still had more than 70K. It was all caused by this problem, which made people feel very collapsed.
Setting the timeout information in python's urllib
Since there is no direct link in python's urllib How to set the timeout, so you need to set the socket timeout information of python
So:
import socket import urllib socket.setdefaulttimeout(9.0) try: content = urllib.urlopen(url).read() exception socket.timeout: pass
In this way, a socket.timeout exception will be thrown after urllib times out when opening the web page. Just put this exception Just catch it and handle it.
Windows 7 Set global path for python
- Select "Computer" from the "Start" menu
- Select "System Properties" from the context menu
- Click the "Advanced System Settings> Advanced" tab
- Click "Environment Variables", then find PATH under "System Variables" and click.
- In the Edit window, modify PATH by adding the location of the class to the value of PATH. If there is no entry PATH, you can choose to add a new variable and then add PATH as the name and the python (python.exe) installation location as the value. For example, install it in D:\python2.7
- Open the "Command" prompt window again, and then run python.