So I have an application written in Python that uses virtualenv with Python 2.7.3. Application runs some tasks with rabbitmq and 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. I started up rabbitmq server and then tried to start python interpreter and I get:
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. I can start global Python without problem. My server (using Linode) wasn't restarted recently and as far as I know nothing was installed/updated. What could have happened?
It looks like virtualenv 20 has a bug in it, so you have to downgrade to an earlier version.
deactivate
. This will either exit your virtualenv or tell you that you weren't in one.pip freeze | grep virtualenv
pip freeze | grep virtualenv
. Is virtualenv in the output with a version of 20.*? If so, this is your issue -- move on to the next step. If not, this may not help.pip install --upgrade virtualenv==16.7.7
. The --upgrade
switch is necessary for both upgrading and downgrading. It'll change your virtualenv to a version known to work.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. used on Unix-like systems. It should be a standard part of all Python environments.
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).
You have a wrong $PYTHONPATH, that probably doesn't contain the path to the virtualenv libs! 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.
Please try first, if python works alone. If you get similar result, it have a wrong PYTHONPATH.
Activate your desired virtualenv and run:
$ 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:
unset PYTHONPATH
Now try again running 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. 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.
The better approach is to use a ".pth" file in your virtualenv.
Unset $PYTHONPATH or remove it from your login (.bash_profile, .bashrc, ...)
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:
/path-to-your-packages/...
Run python and it should start without error and also find your packages
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. Activating another env finds then correct pathes automatically.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.