简体   繁体   English

如何在Ubuntu 10.10上安装Python 2.7的模块?

[英]How to install modules for Python 2.7 on Ubuntu 10.10?

On Ubuntu 10.10, I am unable to install lxml to python 2.7. 在Ubuntu 10.10上,我无法将lxml安装到python 2.7。 Here are the steps I take. 以下是我采取的步骤。

sudo su -
apt-get install python2.7
apt-get install python-lxml

Note when running the install for python-lxml package, the following appeared: 注意在运行python-lxml包的安装时,出现以下内容:

INFO: using unknown version '/usr/bin/python2.7' (debian_defaults not up-to-date?)"

Importing the module in python2.6 (the version that comes standard with Ubuntu) works. 在python2.6中导入模块(Ubuntu标配的版本)可以正常工作。 However, importing the module under python2.7 does not. 但是,在python2.7下导入模块则不行。 So how does one install Python modules to a non-default Python installation? 那么如何将Python模块安装到非默认的Python安装中呢?

Try to install libxml2, libxml2-dev, libxslt, libxslt-dev, python-dev . 尝试安装libxml2, libxml2-dev, libxslt, libxslt-dev, python-dev These are header files. 这些是头文件。 Then try to install lxml again. 然后尝试再次安装lxml

On Ubuntu 10.10 the python packages installed from the repositories get installed to /usr/lib/python2.6/dist-packages so one option is to add this path to your $PYTHONPATH environmental variable so python2.7 will look to the python2.6 directory for the libs. 在Ubuntu 10.10上,从存储库安装的python包安装到/usr/lib/python2.6/dist-packages,因此一个选项是将此路径添加到$ PYTHONPATH环境变量中,以便python2.7查看python2.6 libs的目录。

What I've done on Ubuntu 10.10 is add 我在Ubuntu 10.10上所做的就是添加

export PYTHONPATH="$PYTHONPATH:/usr/lib/python2.6/dist-packages"

to my .bashrc file, and also to my .gnomerc file. 到我的.bashrc文件,还有我的.gnomerc文件。 This sets the $PYTHONPATH for python instances started from the shell or from the gnome desktop. 这为从shell或gnome桌面启动的python实例设置了$ PYTHONPATH。 You should then be able to import the python libs which you have installed from the Ubuntu repositories in python2.7. 然后,您应该能够从python2.7中的Ubuntu存储库导入已安装的python库。

.bashrc and .gnomerc are both located in your home directory; .bashrc和.gnomerc都位于您的主目录中; you might have to create .gnomerc if it doesn't already exist. 如果它还不存在,您可能必须创建.gnomerc。 And one caution: I had a syntax error in my .gnomerc which stopped the gnome desktop from loading, and I couldn't log in. I had to use a recovery console to fix this syntax error and then I could log in again. 还有一点需要注意:我的.gnomerc中出现语法错误,导致gnome桌面停止加载,我无法登录。我不得不使用恢复控制台修复此语法错误,然后我可以再次登录。

This seems a little hackish to me, so I'm interested in hearing better solutions. 这对我来说似乎有些迟钝,所以我有兴趣听听更好的解决方案。

我有一个最简单的技巧只需在搜索框中打开synaptic包管理器类型“python-lxml”它将显示所有依赖项和可用包选择要安装的包并点击应用。

Another solution might be to use the following code: 另一种解决方案可能是使用以下代码:

try:
  from lxml import etree
except ImportError:
  try:
    # Python 2.5
    import xml.etree.cElementTree as etree
  except ImportError:
    try:
      # Python 2.5
      import xml.etree.ElementTree as etree
    except ImportError:
      try:
        # normal cElementTree install
        import cElementTree as etree
      except ImportError:
        try:
          # normal ElementTree install
          import elementtree.ElementTree as etree
        except ImportError:
          print("Failed to import ElementTree from any known place")

[ Source ] [ 来源 ]

This will import lxml if it is available, or the original ElementTree otherwise. 这将导入lxml(如果可用),否则导入原始ElementTree。

I use this code for my application on Google App Engine (using Python 2.7): on the server it will use lxml, on my machine it will use ElementTree. 我在Google App Engine上使用此代码(使用Python 2.7):在服务器上它将使用lxml,在我的机器上它将使用ElementTree。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM