Details about python custom modules and packages

Source: Internet
Author: User
Tags pprint
This article describes the python custom module and package 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, pytest is the package, and add path F: \ test to the environment variable PYTHONPATH. the init. py file in the pytest package is necessary and the content can be empty.

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

The above is a detailed description of the python custom module and package. For more information, see other related articles in the first PHP community!

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.