Python custom modules, packages, and python custom modules
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, 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