import 本地Python module或package
很基础很重要的一课,虽然很简单,但是防止以后忘了,还是记下来
这个笔记里说的都是import本地的,自己创建的,或者复制粘贴的别人的,总之“不是安装到library”的module or package, 所以标题里有个大写的LOCAL
module
what’s a module ?
It’s just a python filewhy do we need?
Because we wanna re-use code, your own code or someone others’dir() 用于在Python interpreter shell中展示当前的namespace,namespace这个词真是好久不见
import LOCAL module
print name会打印这个.py文件的名字
➜ Desktop pwd
/Users/harry/Desktop
➜ Desktop more test_function.py
print __name__
a = 100
def some_func():
print "hello"
print "world"
print a
➜ Desktop
这个例子是引入本地的某个module,module其实是一个.py文件,所以这个例子一定要在同一个目录下做,我选了Desktop目录
➜ Desktop python
Python 2.7.5 (default, Mar 9 2014, 22:15:05)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import test_function # 第一种方式,直接写名字
test_function
>>># 每次引入一个module,你当前的Python interpreter都会把这个module的代码逐行执行,所以这里有一个test_function的输出,因为源文件里有个 print __name__
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'test_function']
>>>
>>> test_function.some_func()
hello
world
100
>>> test_function.b = 77 # 注意这个地方,虽然原来的test_function.py里没有这个叫b的变量,但是其实可以给它赋值
>>>
>>> test_function.b
77
>>>
>>> from test_function import some_func
>>>
>>> some_func()
hello
world
100
>>> dir() # 这次的namespace里就会多了这个some_func()
['__builtins__', '__doc__', '__name__', '__package__', 'some_func', 'test_function']
>>>
- as 关键词,更名被你引入的module
➜ Desktop python
Python 2.7.5 (default, Mar 9 2014, 22:15:05)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import test_function as ppp
test_function # 但是这里的 __name__ 不会变
>>> dir() # 这里的会变
['__builtins__', '__doc__', '__name__', '__package__', 'ppp']
>>>
>>>
>>> ppp.some_func()
hello
world
100
>>>
- what does python do when you import a module
每次引入一个module,你当前的Python interpreter都会把这个module的代码逐行执行,所以这里有一个testfunction的输出,因为源文件里有个 print _name , 但是你在同一个interpreter shell里引入两次,它就不会执行两次
- module creates its own namespace
➜ Desktop python
Python 2.7.5 (default, Mar 9 2014, 22:15:05)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import test_function
test_function
>>>
>>>
>>> test_function.some_func()
hello
world
100
>>>
>>> a = 11
>>> test_function.a
100
>>>
>>> a
11
>>>
- 一个有意思的现象,单独执行那个.py,输出的name 就是main
这样也就有了以后的
if name == ‘main‘:
main()
➜ Desktop python test_function.py
__main__
➜ Desktop
所以在别的interpreter shell或者Python文件中引用它时,这个地方name == ‘main‘: 不成立,所以就不会执行if下面的内容,这就提供了一种很好的机制,可以让我们可以单独执行源文件里的东西
➜ Desktop cat test_function.py
print __name__
a = 100
def some_func():
print "hello"
print "world"
print a
if __name__ == '__main__':
print "this is excuted by itself"
➜ Desktop
➜ Desktop
➜ Desktop python test_function.py
__main__
this is excuted by itself # 这样这句话就出来了
➜ Desktop
package/path
- 先说sys.path, 我的理解是,这些path都是可以直接import的,也就是已经install的module和package所在的地方
pprint.pprint(sys.path)是个好办法,因为直接输出sys.path看着太混乱了
- 注意下面pprint出来的第一行内容,是个空的string,这就代表了Python会查看本地目录,也就是一个Python文件自己所在的目录,也就解释了之前在同一个目录Desktop为什么可以直接引用
➜ Desktop python
Python 2.7.5 (default, Mar 9 2014, 22:15:05)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> import pprint
>>>
>>> pprint.pprint(sys.path)
['',
'/Library/Python/2.7/site-packages/trigger-1.5.2b2-py2.7.egg',
'/Library/Python/2.7/site-packages/redis-2.10.3-py2.7.egg',
'/Library/Python/2.7/site-packages/SimpleParse-2.1.1-py2.7-macosx-10.9-intel.egg',
'/Library/Python/2.7/site-packages/pyparsing-1.5.7-py2.7.egg',
'/Library/Python/2.7/site-packages/pycrypto-2.6.1-py2.7-macosx-10.9-intel.egg',
'/Library/Python/2.7/site-packages/pyasn1-0.1.8-py2.7.egg',
'/Library/Python/2.7/site-packages/IPy-0.83-py2.7.egg',
'/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg',
'/Library/Python/2.7/site-packages/pyPluribus-0.3.0-py2.7.egg',
'/Library/Python/2.7/site-packages',
'/Library/Python/2.7/site-packages/napalm-1.0.0-py2.7.egg',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC',
'/Library/Python/2.7/site-packages']
>>>
- 如何修改sys.path
使用 export命令修改,这个命令也是通用的
为了演示这个过程,我创建了一个文件夹在Desktop, 叫package1
这涉及到了Linux Mac OS X等系统的环境变量的设置,命令是 env ,可以展示当前所有的环境变量,Python的那个值的key是PYTHONPATH
➜ Desktop env | grep PYT #看到目前是没有设置任何PYTHONPATH变量的
➜ Desktop
下面我去创建一个package1,然后尽管这个folder里什么都还没有,它还是能被加到PYTHONPATH中
➜ Desktop mkdir package1
➜ Desktop cd package1
➜ package1
➜ package1
➜ package1 pwd
/Users/harry/Desktop/package1
➜ package1 export PYTHONPATH = /Users/harry/Desktop/package1
zsh: bad assignment
➜ package1 export PYTHONPATH=/Users/harry/Desktop/package1
➜ package1 env | grep PYT
PYTHONPATH=/Users/harry/Desktop/package1
➜ package1
进入Python查看一下
➜ package1 python
Python 2.7.5 (default, Mar 9 2014, 22:15:05)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> import pprint
>>> pprint.pprint(sys.path)
['',
'/Library/Python/2.7/site-packages/trigger-1.5.2b2-py2.7.egg',
'/Library/Python/2.7/site-packages/redis-2.10.3-py2.7.egg',
'/Library/Python/2.7/site-packages/SimpleParse-2.1.1-py2.7-macosx-10.9-intel.egg',
'/Library/Python/2.7/site-packages/pyparsing-1.5.7-py2.7.egg',
'/Library/Python/2.7/site-packages/pycrypto-2.6.1-py2.7-macosx-10.9-intel.egg',
'/Library/Python/2.7/site-packages/pyasn1-0.1.8-py2.7.egg',
'/Library/Python/2.7/site-packages/IPy-0.83-py2.7.egg',
'/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg',
'/Library/Python/2.7/site-packages/pyPluribus-0.3.0-py2.7.egg',
'/Library/Python/2.7/site-packages',
'/Library/Python/2.7/site-packages/napalm-1.0.0-py2.7.egg',
'/Users/harry/Desktop/package1', # <<<<<<<<<<<<<<<<<-------------------------check it out!!!!!!!!
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC',
'/Library/Python/2.7/site-packages']
>>>
- 往这个package1里加点东西,加init.py才算一个package,这个方法3.3版本前适用,后面再说这个文件干啥用的
➜ package1 pwd
/Users/harry/Desktop/package1
➜ package1 touch __init__.py
➜ package1 ls -al
total 0
drwxr-xr-x 3 harry staff 102 Aug 8 12:14 .
drwxr-xr-x+ 26 harry staff 884 Aug 8 12:01 ..
-rw-r--r-- 1 harry staff 0 Aug 8 12:14 __init__.py
➜ package1
其实这个时候,它才能被称为是一个package
这个时候我遇到了一个issue,提示ImportError: No module named package1,但是其实pprint都已经显示了,原因是permission issue,chmod 755 package1/ 就好了
- 往package里价格module,也就是py文件,添加一个p_test.py
➜ Desktop cd package1
➜ package1 vi p_test.py
➜ package1 python p_test.py
hello world, this is excuted by myself
➜ package1
➜ package1
➜ package1 cat p_test.py
def some_func():
print "from some_func"
def another_func():
print "from another func"
if __name__ == '__main__':
print "hello world, this is excuted by myself"
➜ package1
在Python interpreter shell中引用这个p_test.py,引用的格式是 import some_package.some_module
➜ Desktop python
Python 2.7.5 (default, Mar 9 2014, 22:15:05)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import package1.p_test
>>>
>>> package1.p_test.some_func()
from some_func
>>>
package1下面还可以创建多个子目录,依然不用修改sys.path,多个子目录里就可以放自己想要的module
编辑 init.py ,这里体现了这货有啥用
先往里随便加点print
➜ package1 cat __init__.py
print "In __init__.py"
当你再次引用时, 会看到一旦引用,Python会执行init里的print,其实Python会逐条执行这个init,虽然里面可以什么都没有
现在dir()展开一下namespace会发现没有p_test存在,如果想用p_test,必须单独import,init这个文件就是为了解决这个问题的
➜ Desktop python
Python 2.7.5 (default, Mar 9 2014, 22:15:05)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import package1
In __init__.py
>>>
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'package1']
>>> dir(package1)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__']
>>>
发现没有p_test存在,如果想用p_test,必须单独import
>>> package1.p_test
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'p_test'
>>>
>>>
>>> import package1.p_test
>>>
>>> package1.p_test
<module 'package1.p_test' from 'package1/p_test.pyc'>
>>>
>>> package1.p_test.some_func()
from some_func
>>>
> - 再次编辑 __init__.py “连接”你想要的file
```sh
➜ package1 vi __init__.py
➜ package1
➜ package1 cat __init__.py
print "In __init__.py"
from . import p_test # from . 的意思是从当前目录import
这样ptest就被_init 引用了
再次回到Python,再引用一次package1
namespace都在了
➜ Desktop python
Python 2.7.5 (default, Mar 9 2014, 22:15:05)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import package1
In __init__.py
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'package1']
>>> dir(package1)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', 'p_test']
>>> dir(package1.p_test)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'another_func', 'some_func']
>>>
p_test这个module也可以用了
>>>
>>> package1.p_test.some_func() # 可以直接这么用
from some_func
>>>
- 关于import现成的module or package
1) 以pprint为例
locate pprint可以找到pprint.py在哪里,一般是在下面这个,看到这个pprint.py就可以认为这是一个 module
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/pprint.py
打开看一下他其实就是一个py文件,里面是源码,有作者的info comment等等
2) 以paramiko为例
locate paramiko找到一个叫paramiko的文件夹,在/Library/Python/2.7/site-packages/paramiko 这个路径
进去看一下会发现,这是一个 package
你可以看到它的 init.py 这个文件,其实你import paramiko的时候,就是它在连接它的各个py文件
➜ paramiko pwd
/Library/Python/2.7/site-packages/paramiko
➜ paramiko ls *.py
__init__.py common.py kex_group14.py resource.py sftp_si.py
_version.py compress.py kex_gss.py rsakey.py ssh_exception.py
_winapi.py config.py message.py server.py ssh_gss.py
agent.py dsskey.py packet.py sftp.py transport.py
auth_handler.py ecdsakey.py pipe.py sftp_attr.py util.py
ber.py file.py pkey.py sftp_client.py win_pageant.py
buffered_pipe.py hostkeys.py primes.py sftp_file.py
channel.py kex_gex.py proxy.py sftp_handle.py
client.py kex_group1.py py3compat.py sftp_server.py
➜ paramiko
一共有42个py文件
➜ paramiko ls *.py | wc -l
42
➜ paramiko
从其他地方看到的关于import的内容
http://stackoverflow.com/questions/6757192/importing-a-function-from-a-class-in-another-file
以上似乎是直接引用,不需要安装
Head First 上40 - 41页上讲的是如何安装然后/Library/Python/2.7/site-packages就有了相应的module,在程序里直接 import那个名字即可
卸载
http://stackoverflow.com/questions/1550226/python-setup-py-uninstall
思考:
Head First 上讲的是引用一个print多层数组的方法,引用的是一个含有一个方法的.py文件,如果是类的话,用install的话,应该和Head First上的方法是一样的,如果想直接从文件引用的话,就参考第一个stackoverflow的链接,似乎需要在源文件里 always run着一个instance