简体   繁体   English

无法启动 Python - ImportError: No module named posixpath

[英]Can't start Python - ImportError: No module named posixpath

So I have an application written in Python that uses virtualenv with Python 2.7.3.所以我有一个用 Python 编写的应用程序,它在 Python 2.7.3 中使用 virtualenv。 Application runs some tasks with rabbitmq and celery.应用程序使用 rabbitmq 和 celery 运行一些任务。 Everything was running smooth for couple days and today I noticed tasks are not being processed.几天来一切都很顺利,今天我注意到任务没有被处理。 I logged in to the server and noticed that rabbitmq is down.我登录到服务器并注意到rabbitmq 已关闭。 I started up rabbitmq server and then tried to start python interpreter and I get:我启动了rabbitmq服务器,然后尝试启动python解释器,我得到:

Traceback (most recent call last):
  File "/home/marcin/pyProjects/resbit/venv/lib/python2.7/site.py", line 67, in <module>
    import os
  File "/home/marcin/pyProjects/resbit/venv/lib/python2.7/os.py", line 49, in <module>
    import posixpath as path
ImportError: No module named posixpath

This only happens within virtualenv.这仅发生在 virtualenv 中。 I can start global Python without problem.我可以毫无问题地启动全局 Python。 My server (using Linode) wasn't restarted recently and as far as I know nothing was installed/updated.我的服务器(使用 Linode)最近没有重新启动,据我所知没有安装/更新。 What could have happened?可能发生了什么?

It looks like virtualenv 20 has a bug in it, so you have to downgrade to an earlier version.看起来 virtualenv 20 有一个错误,所以你必须降级到早期版本。

  1. Make sure you aren't in a virtualenv by running deactivate .通过运行deactivate确保您不在virtualenv 中。 This will either exit your virtualenv or tell you that you weren't in one.这将退出您的 virtualenv 或告诉您您不在其中。
  2. Run pip freeze | grep virtualenv运行pip freeze | grep virtualenv pip freeze | grep virtualenv . pip freeze | grep virtualenv Is virtualenv in the output with a version of 20.*?输出中的virtualenv 是20.* 版本吗? If so, this is your issue -- move on to the next step.如果是这样,这就是您的问题——继续下一步。 If not, this may not help.如果没有,这可能无济于事。
  3. Run pip install --upgrade virtualenv==16.7.7 .运行pip install --upgrade virtualenv==16.7.7 The --upgrade switch is necessary for both upgrading and downgrading. --upgrade开关对于升级和降级都是必需的。 It'll change your virtualenv to a version known to work.它会将您的 virtualenv 更改为已知可以工作的版本。

Your virtual environment has clearly been created incorrectly or modified after creation.您的虚拟环境显然创建不正确或在创建后被修改。 posixpath is a built-in module, which is one of the dynamically selected alternatives for the os.path module. posixpath是一个内置模块,它是os.path模块的动态选择的替代方案之一。 used on Unix-like systems.在类 Unix 系统上使用。 It should be a standard part of all Python environments.它应该是所有 Python 环境的标准部分。

In [2]: import posixpath

In [3]: import os.path

In [4]: os.path is posixpath
Out[4]: True

If you can't import posixpath the implication is you are either somehow selecting the wrong platform or your environment does not contain it (meaning it's incorrectly installed).如果您无法导入posixpath则意味着您以某种方式选择了错误的平台,或者您的环境不包含它(意味着它安装不正确)。

You have a wrong $PYTHONPATH, that probably doesn't contain the path to the virtualenv libs!您输入了错误的 $PYTHONPATH,它可能不包含 virtualenv 库的路径! As soon as you set $PYTHONPATH, pathes are used only as given here and implicit pathes to libs from your virtualenv are not considered anymore if not explicitly defined in $PYTHONPATH.一旦你设置了 $PYTHONPATH,路径就只使用这里给出的路径,如果没有在 $PYTHONPATH 中明确定义,则不再考虑从你的 virtualenv 到库的隐式路径。

Please try first, if python works alone.如果 python 单独工作,请先尝试。 If you get similar result, it have a wrong PYTHONPATH.如果你得到类似的结果,它有一个错误的 PYTHONPATH。

Activate your desired virtualenv and run:激活您想要的 virtualenv 并运行:

$ python
Traceback (most recent call last):
File "/data/appl/mmtools/lib/python2.7/site-packages/site.py", line 74, in <module>
__boot()
File "/data/appl/mmtools/lib/python2.7/site-packages/site.py", line 2, in __boot
import sys, os, os.path
File "/data/appl/py27/lib64/python2.7/os.py", line 49, in <module>
import posixpath as path
ImportError: No module named posixpath

Then unset $PYTHONPATH:然后取消设置 $PYTHONPATH:

unset PYTHONPATH

Now try again running python现在再次尝试运行 python

Python 2.7.5 (default, Mar 26 2019, 22:13:06)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

If you have the same problem, you need to carefully check $PYTHONPATH.如果你有同样的问题,你需要仔细检查 $PYTHONPATH。 Keep in mind: If you want to switch between multiple virtualenv's, you have to always update your $PYTHONPATH accordingly by adding/exchanging the pathes of virtualenv AND your package path, because it is not automatically done when activating another virtualenv.请记住:如果您想在多个 virtualenv 之间切换,您必须始终通过添加/交换 virtualenv 的路径和您的包路径来相应地更新您的 $PYTHONPATH,因为在激活另一个 virtualenv 时它不会自动完成。

The better approach is to use a ".pth" file in your virtualenv.更好的方法是在您的 virtualenv 中使用“.pth”文件。

  1. Unset $PYTHONPATH or remove it from your login (.bash_profile, .bashrc, ...)取消设置 $PYTHONPATH 或从您的登录名中删除它(.bash_profile、.bashrc、...)

  2. Create a file in your virtualenv, eg $VIRTUALENV/lib/python2.7/site-packages/mypackages.pth with one line per path to your packages as they were in $PYTHONPATH:在您的 virtualenv 中创建一个文件,例如 $VIRTUALENV/lib/python2.7/site-packages/mypackages.pth,每个包路径一行,就像它们在 $PYTHONPATH 中一样:

     /path-to-your-packages/...
  3. Run python and it should start without error and also find your packages运行 python,它应该可以正常启动并找到你的包

If you have different package versions for various virtualenv, you can define correct pathes for each virtualenv in your '.pth' file and you shouldn't define nor need $PYTHONPATH anymore.如果您对各种 virtualenv 有不同的包版本,您可以在 '.pth' 文件中为每个 virtualenv 定义正确的路径,并且您不应再定义或需要 $PYTHONPATH。 Activating another env finds then correct pathes automatically.激活另一个 env 会自动找到正确的路径。

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

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