lxml,flat_namespace并使用或不使用python启动程序

时间:2013-08-03 23:50:49

标签: python command-line-interface lxml

嗯,我有一个有趣的问题,我不明白为什么,但我很乐意。

→ python -V
Python 2.7.1

让我们检查一下我的程序是如何开始的

→ head -2 myprog.py
#!/usr/bin/env python2.7
# encoding: utf-8

让我们检查它返回的内容

→ /usr/bin/env python2.7
Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 

好到目前为止一切顺利。让我们尝试执行代码。

→ python myprog.py -h
Traceback (most recent call last):
  File "/Users/foobar/myprog.py", line 16, in <module>
    from lxml.html import html5parser
  File "/Library/Python/2.7/site-packages/lxml/html/__init__.py", line 42, in <module>
    from lxml import etree
ImportError: dlopen(/Library/Python/2.7/site-packages/lxml/etree.so, 2): Symbol not found: _htmlParseChunk
  Referenced from: /Library/Python/2.7/site-packages/lxml/etree.so
  Expected in: flat namespace
 in /Library/Python/2.7/site-packages/lxml/etree.so
嗯,好的。 lxml ...有很多其他stackoverflow谈论这个特定的问题。 但是很奇怪,因为我有另一个脚本与同一个模块完美配合。所以我决定看看我使用其他脚本的方式,我看到我是直接启动它。

→ ~/myprog.py -h
usage: myprog.py [-h] -u USERNAME [-o ARCHIVEPATH] [-f FILE_FORMAT]

Export html content from Zorglub

所以当我使用python启动它时,它不起作用。当我直接使用该程序时,它正在工作。 为什么?房间里的蟒蛇专家?

1 个答案:

答案 0 :(得分:0)

所以@torek你把我放在正确的车道上。

→ ls -l /usr/bin/python*

哪个给出了

-rwxr-xr-x 2 root 62 2011-08-15 21:09 /usr/bin/python*
-rwxr-xr-x 6 root  1 2011-08-15 21:09 /usr/bin/python-config*
lrwxr-xr-x 1 root  1 2011-08-15 21:09 /usr/bin/python2.5 -> ../../System/Library/Frameworks/Python.framework/Versions/2.5/bin/python2.5*
lrwxr-xr-x 1 root  1 2011-08-15 21:09 /usr/bin/python2.5-config -> ../../System/Library/Frameworks/Python.framework/Versions/2.5/bin/python2.5-config*
lrwxr-xr-x 1 root  1 2011-08-15 21:09 /usr/bin/python2.6 -> ../../System/Library/Frameworks/Python.framework/Versions/2.6/bin/python2.6*
lrwxr-xr-x 1 root  1 2011-08-15 21:09 /usr/bin/python2.6-config -> ../../System/Library/Frameworks/Python.framework/Versions/2.6/bin/python2.6-config*
lrwxr-xr-x 1 root  1 2011-08-15 21:09 /usr/bin/python2.7 -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7*
lrwxr-xr-x 1 root  1 2011-08-15 21:09 /usr/bin/python2.7-config -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7-config*
-rwxr-xr-x 2 root 62 2011-08-15 21:09 /usr/bin/pythonw*
lrwxr-xr-x 1 root  1 2011-08-15 21:09 /usr/bin/pythonw2.5 -> ../../System/Library/Frameworks/Python.framework/Versions/2.5/bin/pythonw2.5*
lrwxr-xr-x 1 root  1 2011-08-15 21:09 /usr/bin/pythonw2.6 -> ../../System/Library/Frameworks/Python.framework/Versions/2.6/bin/pythonw2.6*
lrwxr-xr-x 1 root  1 2011-08-15 21:09 /usr/bin/pythonw2.7 -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/pythonw2.7*

好吧,试试吧

→ /usr/bin/python

我们得到了

Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:06) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from lxml import etree
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: dlopen(/Library/Python/2.7/site-packages/lxml/etree.so, 2): Symbol not found: _htmlParseChunk
  Referenced from: /Library/Python/2.7/site-packages/lxml/etree.so
  Expected in: flat namespace
 in /Library/Python/2.7/site-packages/lxml/etree.so
>>> 

然后另一个

→ /System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7

我们得到了

Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from lmxl import etree
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named lmxl
>>> 

哦,让事情稍差...... :) python3在/ usr / local / bin /

→ ls -l /usr/local/bin/python*
-rwxr-xr-x 1 root 2 2006-11-09 05:05 /usr/local/bin/python2.5-config*
lrwxr-xr-x 1 root 1 2013-02-25 12:50 /usr/local/bin/python3 -> ../../../Library/Frameworks/Python.framework/Versions/3.3/bin/python3*
lrwxr-xr-x 1 root 1 2013-02-25 12:50 /usr/local/bin/python3-32 -> ../../../Library/Frameworks/Python.framework/Versions/3.3/bin/python3-32*
lrwxr-xr-x 1 root 1 2013-02-25 12:50 /usr/local/bin/python3-config -> ../../../Library/Frameworks/Python.framework/Versions/3.3/bin/python3-config*
lrwxr-xr-x 1 root 1 2013-02-25 12:50 /usr/local/bin/python3.3 -> ../../../Library/Frameworks/Python.framework/Versions/3.3/bin/python3.3*
lrwxr-xr-x 1 root 1 2013-02-25 12:50 /usr/local/bin/python3.3-32 -> ../../../Library/Frameworks/Python.framework/Versions/3.3/bin/python3.3-32*
lrwxr-xr-x 1 root 1 2013-02-25 12:50 /usr/local/bin/python3.3-config -> ../../../Library/Frameworks/Python.framework/Versions/3.3/bin/python3.3-config*
lrwxr-xr-x 1 root 1 2013-02-25 12:50 /usr/local/bin/python3.3m -> ../../../Library/Frameworks/Python.framework/Versions/3.3/bin/python3.3m*
lrwxr-xr-x 1 root 1 2013-02-25 12:50 /usr/local/bin/python3.3m-config -> ../../../Library/Frameworks/Python.framework/Versions/3.3/bin/python3.3m-config*
-rwxr-xr-x 1 root 1 2012-03-02 13:43 /usr/local/bin/pythonbrew_install*
lrwxr-xr-x 1 root 1 2013-02-25 12:50 /usr/local/bin/pythonw3 -> ../../../Library/Frameworks/Python.framework/Versions/3.3/bin/pythonw3*
lrwxr-xr-x 1 root 1 2013-02-25 12:50 /usr/local/bin/pythonw3-32 -> ../../../Library/Frameworks/Python.framework/Versions/3.3/bin/pythonw3-32*
lrwxr-xr-x 1 root 1 2013-02-25 12:50 /usr/local/bin/pythonw3.3 -> ../../../Library/Frameworks/Python.framework/Versions/3.3/bin/pythonw3.3*
lrwxr-xr-x 1 root 1 2013-02-25 12:50 /usr/local/bin/pythonw3.3-32 -> ../../../Library/Frameworks/Python.framework/Versions/3.3/bin/pythonw3.3-32*

我们还没有,但我至少有两份python 2.7