Python Sys.path Details _python

Source: Internet
Author: User

How do I add a path "permanent" to Sys.path?

Sys.path is the path set of the Python search module, a list

Copy Code code as follows:

[', ' c:\\windows\\system32\\python26.zip ', ' c:\\python26\\dlls ', ' c:\\python26\ \lib ', ' c:\\python26\\lib\\ Plat-win ', ' c:\\python26\\lib\\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 a Python environment, but the path you add yourself after exiting the Python environment automatically disappears!

Modifying in a Python script

Copy Code code as follows:

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

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

1. Place your own py file in the Site_packages directory:

The following command shows the Site-packages directory:

Copy Code code as follows:

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

But doing so can lead to the problem that all kinds of modules are put into this folder, which can lead to chaos, which is obvious.

Note that you do not create subfolders and then place your own modules in a subfolder to resolve the problem, which can cause errors when you use the import statement.


2. Use the PTH file, create a. pth file in the Site-packages file, write the path of the module, a line of paths, the following is an example, the PTH file can also use the annotation:

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

This is a good idea, but there are administrative problems and cannot be shared in different versions of Python.

3. Using the PYTHONPATH environment variable, enter the relevant path in this environment variable, with a comma between the different paths (English!). Apart, if the Pythonpath variable does not exist, you can create it!

Paths are automatically added to the Sys.path and can be shared in different versions of Python, which should be the easiest way to do it.

For information about the environment variables associated with Python, please refer to:

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.