This article describes the python custom module and package 1. save a hello. py file in the F:/data/python directory.
>>> def hello(x): print x
>>> 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!