Pythonsys. path-Python tutorial

Source: Internet
Author: User
This article details Pythonsys. path. if you need it, you can refer to how to add the path "permanent" to sys. path?

Sys. path is the path set of the python Search module and is a list

The code is as follows:


['', 'C: \ WINDOWS \ system32 \ python26.zip ', 'C: \ Python26 \ DLLs', 'C: \ Python26 \ lib ', 'C: \ Python26 \ lib \ plat-win', 'C: \ Python26 \ lib-tk ', 'C: \ python26 ', 'C: \ Python26 \ lib \ site-packages ', 'C: \ Python26 \ lib \ site-packages \ win32', 'C: \ Python26 \ lib \ site-packages \ win32 \ lib ', 'C: \ Python26 \ lib \ site-packa ges \ Pythonwin']

You can use sys. path. append (path) to add related paths in the python Environment. However, after exiting the python environment, the paths you add automatically disappear!

Modify in python script

The code is as follows:


Import sys
Sys. path. append ('C: \ mypythonlib ')

To solve this problem, you can use the following methods:

1. put your own py file in the site_packages Directory:

The following command displays the site-packages directory:

The code is as follows:


Python-c "from distutils. sysconfig import get_python_lib; print get_python_lib ()"

However, this will cause a problem, that is, if all the modules are placed in this folder, it will lead to chaos, which is obvious.

Note: If you do not create a subfolder, place your modules in the subfolder to solve the problem. this will cause an error when using the import statement.


2. use the pth file, create a. pth file in the site-packages file, write the path of the module into it, and create a path in one line. The following is an example. you can also use annotations for the pth file:

#. Pth file for the my project (this line is a comment)
E: \ DjangoWord
E: \ DjangoWord \ mysite
E: \ DjangoWord \ mysite \ polls

This is a good method, but there are management problems and cannot be shared in different python versions.

3. use the PYTHONPATH environment variable and enter related paths in this environment variable. separate paths with commas !) Separated. if the PYTHONPATH variable does not exist, you can create it!

The path is automatically added to sys. path and can be shared in different python versions. it should be the same convenient method.

For python-related environment variables, see:

Http://docs.python.org/using/cmdline.html

Find PYTHONPATH on the page

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.