创建本地副本后可以避免解释器找不到模块的情况。
1. 创建一个测试用的pl.py
1 def printTest(): 2 print("this is a test")
2. 将pl.py放入pl文件夹中,并在pl文件夹中再创建一个setup.py
1 from distutils.core import setup 2 3 setup( 4 name='pl', 5 version ='1.0.0', 6 py_modules=['pl'], 7 author='htest', 8 author_email='htest@gmail.com', 9 url='http://www.test.com', 10 description='this is a test', 11 )
3. 构建发布文件--进入pl目录,并在cmd中输入python setup.py sdist
1 D:python estpl>python setup.py sdist 2 running sdist 3 running check 4 warning: sdist: manifest template 'MANIFEST.in' does not exist (using default file list) 5 6 warning: sdist: standard file not found: should have one of README, README.txt 7 8 writing manifest file 'MANIFEST' 9 creating pl-1.0.0 10 making hard links in pl-1.0.0... 11 hard linking pl.py -> pl-1.0.0 12 hard linking setup.py -> pl-1.0.0 13 creating dist 14 creating 'distpl-1.0.0.zip' and adding 'pl-1.0.0' to it 15 adding 'pl-1.0.0PKG-INFO' 16 adding 'pl-1.0.0pl.py' 17 adding 'pl-1.0.0setup.py' 18 removing 'pl-1.0.0' (and everything under it)
4. 创建本地副本--同样在pl目录,并在cmd中输入python setup.py install
1 D:python estpl>python setup.py install 2 running install 3 running build 4 running build_py 5 creating build 6 creating buildlib 7 copying pl.py -> buildlib 8 running install_lib 9 copying buildlibpl.py -> C:UsershuangchAppDataLocalProgramsPythonPython35Libsite-packages 10 byte-compiling C:UsershuangchAppDataLocalProgramsPythonPython35Libsite-packagespl.py to pl.cpython-35.pyc running install_egg_info Writing C:UsershuangchAppDataLocalProgramsPythonPython35Libsite-packagespl-1.0.0-py3.5.egg-info
5 测试
重新打开一个IDLE
1 >>> import pl 2 >>> pl.printTest() 3 this is a test //验证成功 4 >>>
6 以上内容完成后,就可以在任意的py文件中,来引用pl。同时还可以将pl上传到PypI社区,来分享代码。