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

发布于 2024-10-18 20:22:03 字数 396 浏览 6 评论 0原文

在 Ubuntu 10.10 上,我无法将 lxml 安装到 python 2.7。这是我采取的步骤。

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

请注意,在运行 python-lxml 包的安装时,出现以下内容:

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

在 python2.6(Ubuntu 标配版本)中导入模块有效。但是在python2.7下导入模块则不行。那么如何将 Python 模块安装到非默认 Python 安装中呢?

On Ubuntu 10.10, I am unable to install lxml to 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:

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. However, importing the module under python2.7 does not. So how does one install Python modules to a non-default Python installation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

贵在坚持 2024-10-25 20:22:03

尝试安装libxml2、libxml2-dev、libxslt、libxslt-dev、python-dev。这些是头文件。然后尝试再次安装lxml

Try to install libxml2, libxml2-dev, libxslt, libxslt-dev, python-dev. These are header files. Then try to install lxml again.

樱桃奶球 2024-10-25 20:22:03

在 Ubuntu 10.10 上,从存储库安装的 python 软件包安装到 /usr/lib/python2.6/dist-packages,因此一种选择是将此路径添加到 $PYTHONPATH 环境变量中,以便 python2.7 将查找 python2.6库的目录。

我在 Ubuntu 10.10 上所做的是将其添加

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

到我的 .bashrc 文件和 .gnomerc 文件中。这会为从 shell 或 gnome 桌面启动的 python 实例设置 $PYTHONPATH。然后,您应该能够导入从 python2.7 中的 Ubuntu 存储库安装的 python 库。

.bashrc 和 .gnomerc 都位于您的主目录中;如果 .gnomerc 尚不存在,您可能需要创建它。还有一个警告:我的 .gnomerc 中有一个语法错误,导致 gnome 桌面无法加载,而且我无法登录。我必须使用恢复控制台来修复此语法错误,然后我才能再次登录。

这对我来说似乎有点老套,所以我有兴趣听到更好的解决方案。

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.

What I've done on Ubuntu 10.10 is add

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

to my .bashrc file, and also to my .gnomerc file. This sets the $PYTHONPATH for python instances started from the shell or from the gnome desktop. You should then be able to import the python libs which you have installed from the Ubuntu repositories in python2.7.

.bashrc and .gnomerc are both located in your home directory; you might have to create .gnomerc if it doesn't already exist. 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.

This seems a little hackish to me, so I'm interested in hearing better solutions.

何以畏孤独 2024-10-25 20:22:03

另一种解决方案可能是使用以下代码:

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]

这将导入 lxml(如果可用),或者否则是原始的 ElementTree。

我在 Google App Engine 上的应用程序中使用此代码(使用 Python 2.7):在服务器上它将使用 lxml,在我的计算机上它将使用 ElementTree。

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.

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.

电影里的梦 2024-10-25 20:22:03

我有一个最简单的技巧,只需打开突触包管理器,在搜索框中输入“python-lxml”,它就会向您显示所有依赖项和可用的包,选择您想要安装的包并点击“应用”。

I have one easiest trick Just open synaptic package manager type "python-lxml" in search box it will show you all the dependencies and available packages select packages which you want to install and hit apply.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文