Python custom modules, packages, and python custom modules

Source: Internet
Author: User
Tags pprint

Python custom modules, packages, and python custom modules

1. Save a hello. py file in the F:/data/python directory.

  • Hello. py
>>> def hello(x):    print x
  • Directory

  • Import
>>> Import sys >>> sys. path. append ('f:/data/python') # Add a path, which is one-time >>> import hello >>> hello. hello (5) # Call 5

2. Import the custom module without changing sys. path:

Method 1: place the hello2.py file in the D:/Python27/lib/site-packages directory.

>>> import sys,pprint>>> pprint.pprint(sys.path)['', 'D:\\Python27\\Lib\\idlelib', 'F:\\data\\s', 'D:\\Python27', 'C:\\Windows\\system32\\python27.zip', 'D:\\Python27\\DLLs', 'D:\\Python27\\lib', 'D:\\Python27\\lib\\plat-win', 'D:\\Python27\\lib\\lib-tk', 'D:\\Python27\\lib\\site-packages']>>> import hello2>>> hello2.hello()hello,world!

Method 2: add the F:/data/python path to the environment variable (in the system variable, add the PYTHONPATH variable, and then add the path to the value)

>>> import sys>>> import hello>>> hello.hello(1)1

3. Custom package and import module

First, create the following directory, with pytest as the package, and add path F: \ test to the environment variable PYTHONPATH. In the pytest package, _ init __. the py file is necessary and the content can be blank

>>> import sys>>> from pytest import hello>>> hello.hello(4)4

 

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.