4

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?

marcin_koss
  • 5,375
  • 8
  • 40
  • 56

3 Answers3

4

It looks like virtualenv 20 has a bug in it, so you have to downgrade to an earlier version.

  1. Make sure you aren't in a virtualenv by running deactivate. This will either exit your virtualenv or tell you that you weren't in one.
  2. Run 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.
  3. Run 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.
KevinG
  • 684
  • 2
  • 14
  • 29
Vimal Raj
  • 41
  • 3
1

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).

holdenweb
  • 24,217
  • 7
  • 45
  • 67
0

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.

  1. Unset $PYTHONPATH or remove it from your login (.bash_profile, .bashrc, ...)

  2. Create a file in your virtualenv, e.g. $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/...
    
  3. 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.