0

I am tasked with setting up a Django environment on a RHEL 7 virtual machine. (I am from a Windows background so this is a challenge.) I installed Python 2.7.13 32-bit and pip alongside the default 2.7.5 64-bit, as per this tutorial https://tecadmin.net/install-python-2-7-on-centos-rhel/.

This seems fine - I can access 2.7.5 using python and 2.7.13 using python2.7

I then created a virtual environment sudo virtualenv -p /usr/local/bin/python2.7 venv pointing at 2.7.13, with the following output...

Running virtualenv with interpreter /usr/local/bin/python2.7 
New python executable in /home/butterp/venv/bin/python2.7 
Also creating executable in /home/butterp/venv/bin/python 
Installing setuptools, pip, wheel...done.

Now, after activating the venv, python or python2.7 initiates the 2.7.13 interpreter, which is what I want.

I merrily installed my requirements using sudo pip, as I get permission errors without sudo, which seemed to go well. However, the packages are not available when I try to import them in Python

(venv) [butterp@dev ~]$ python
Python 2.7.13 (default, Sep  8 2017, 03:33:09)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named django

The packages were installed to the base install of python2.7 /usr/lib/python2.7/site-packages rather than the venv, and I CAN import them if I use sudo python or sudo python2.7, although both commands take me to version 2.7.5!

[butterp@dev ~]$ sudo python
Python 2.7.5 (default, May  3 2017, 07:55:04)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-14)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>>

It looks like an error stemming from my misuse of sudo and/or pip, but I am stuck at Confusion Central and unsure how to rectify the situation. Can anyone help? Cheers!

  • Possible duplicate of [ImportError: No module named 'pandas' (inside virtualenv)](https://stackoverflow.com/questions/45666097/importerror-no-module-named-pandas-inside-virtualenv) – phd Jan 19 '18 at 15:46
  • 1
    Don't use sudo in a virtualenv — `sudo pip install` installs packages using global `python`, not in venv. – phd Jan 19 '18 at 15:46
  • @phd thanks. I used sudo because I got permissions error if I didn't. So how do I create an environment where I can use pip without sudo? – PhilFlam Jan 19 '18 at 20:53
  • Did you have permissions problem in `/home/butterp`? Perhaps a result of using `sudo -H`. Fix it with `sudo chown -R butterp /home/butterp` – phd Jan 19 '18 at 21:55
  • @phd I never did `sudo -H`, but tried the fix. Still get same `pip` permission error in venv is `IOError: [Errno 13] Permission denied: '/usr/local/lib/python2.7/lib-dynload/Python-2.7.13-py2.7.egg-info'`. – PhilFlam Jan 19 '18 at 22:48
  • You have your virtual env in `/home/butterp/venv/bin/python2.7`, there shouldn't be attempt to write to `/usr/local/lib/python2.7`. Have you activated the virtual env? – phd Jan 19 '18 at 23:02
  • @phd Yes I activated venv, but I think it's because that's where it's locating `pip`. Although I didn't make a note of it, I probably did `sudo python2.7 get-pip.py`. Is this causing the problem? – PhilFlam Jan 19 '18 at 23:39
  • The rule is simple — when you work with `/usr/local/lib/python2.7` you use `sudo` and in `/home/butterp` you don't. Especially avoid `sudo` when you're working with virtualenv. – phd Jan 21 '18 at 15:41

0 Answers0