Python imports templates for other catalogs

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 and is a list

You can use Sys.path.append (path) to add related paths in a Python environment, but the paths you add automatically disappear when you exit the Python environment!

You can use the following command to enter the search path for the current Python:

Python-c "Import Sys;print" current Python is: ' +sys.prefix;print ' \ n '. Join (Sys.path) "

Practice using the Sys.path.append method to add a path that shows that exiting Python will disappear!

Python-c "Import Sys;print" current Python is: ' +sys.prefix; sys.path.append (R ' E:\DjangoWord ');p rint ' \ n '. Join (Sys.path) "

Run again and you will find yourself adding the path E:\DjangoWord () does not exist!

Python-c "Import Sys;print" current Python is: ' +sys.prefix;print ' \ n '. Join (Sys.path) "

To solve this problem, the following methods can be used:


Place your own py file in the Site_packages directory:

The following command shows the Site-packages directory:

Python-c "fromdistutils.sysconfig import get_python_lib; Print get_python_lib ()"

However, this can lead to a problem, that is, the various types of modules placed in this folder, will lead to a disorderly problem, this is obvious.

Note that you do not create subfolders, and then place your own modules in subfolders to resolve the issue, which results in an error when using the import statement.


Using the PTH file, create the. pth file in the Site-packages file, write the path to the module, one line at a path, the following is an example, the PTH file can also use annotations:

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

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

I'm using the 2nd method.

Python imports templates for other catalogs

Related Article

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.