Referencing custom modules in Python

Source: Internet
Author: User

Learning python for a short period of time, always stuck in a simple grammar, can't really appreciate the power of Python. Today we learned the reference method for custom modules. When referring to the module, the compiler will first in the current directory, then go to Sys.path, Python's installation directory to find the module you refer to, if not, will be an error.

In the first case, your module is in the same directory as the module you want to use. hello.py is a module that has a method hello () that prints Hello, world. test.py is a test program that references the Hello () method in hello.py. Because it is in the same directory as the module, it can be referenced directly.

hello.py

def hello ():    print "Hello World"
test.py
Import Hellohello.hello ()

In the second case, the module is somewhere else. Method One: First refer to the SYS, modify the Sys.path, in fact, the path of the module is added to the Sys.path.

test.py

Import syssys.path.append (' e:/workspace/modules ') import Hellohello.hello ()

Method Two: Is to add the path to the environment variable, this is permanent, you can create a new folder dedicated to the module, the path to the folder to add to the environment variables, can be a labor, never trouble, but also easy to manage.

Referencing custom modules in Python

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.