Optional environment variable settings in Python

Source: Internet
Author: User


A good memory than a rotten pen, see Python Optional variables this piece, the learning of things to record down


The students who write JAVA Hello world must know how to set the environment variables, such as: Java_home,classpath and so on.


Python also has similar environment variables, some are optional (such as: PYTHONPATH, Pythonstartup, etc.)

    1. Set Pythonpath

What is the use of this environment variable?

We know that when importing a module (e.g., import some_module_name), Python needs to find the corresponding module in some path (that is, the path in the Sys.path list), and Pythonpath this variable is to tell Python that its value is also added to the search path (the value that will be Pythonpath is merged into the Sys.path list).

Export pythonpath=/tmppython-c "Import sys; print ' found ' if '/tmp ' in Sys.path Else ' "

The output is: found

Description/TMP was found in Sys.path (that is,/tmp attached to Sys.path)


2. Set Pythonstartup

If the value of this environment variable is a readable file, then the Python command in the file executes before the interactive mode starts.

echo "Print ' Hello World '" >/tmp/a.pyexport Pythonstartup=/tmp/a.pypython

After starting the Python interactive mode, you will see that Hello world has been output.


3. Set Pythonhome

In the case where this variable is not set, Sys.path in Python lists the search paths for some modules.

# for example: [' ', '/usr/lib/python2.7 ', '/usr/lib/python2.7/plat-linux2 ', '/usr/lib/python2.7/lib-tk ', '/usr/lib/python2.7/' Lib-old ', '/usr/lib/python2.7/lib-dynload ', '/usr/local/lib/python2.7/dist-packages ', '/usr/lib/python2.7/ Dist-packages ']

The search path of the Python module is usually sys.prefix/lib by default, and you can see that many paths are/usr/lib/... This, where/usr is the value of the variable sys.prefix (Windows may be the directory where you install Python, such as: C:\Python2.7).

If Pythonhome is manually specified, then the value of Sys.prefix is replaced.


Replacing the value of pythonhome may cause Python to fail to start, back up the value of Sys.prefix before replacing it



4. Set Pythoncaseok

This variable is only valid for Windows and can be set to any value (for example: Set Pythoncaseok=ok)

After setting up Python's import of the module, the case is ignored (ignoring the case for the user module only, and built-in module or case sensitive)



5. Set Pythonioencoding

Looking at this name, IO encoding can guess that this thing is related to input and output. Yes, the function of this environment variable is to specify the encoding of the Python program's standard input and output (Stdin,stdout,stderr).

For the use of this thing, please refer to: http://www.soimort.org/posts/118/ too long don't look :D



That ' s it!650) this.width=650; "src=" Http://img.baidu.com/hi/ldw/w_0050.gif "alt=" W_0050.gif "/>

Optional environment variable settings in Python

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.