To me who was confused because sudo python did not work when using virtualenv

Operation check environment

What is virtualenv?

It is a function that allows you to create multiple Python environments on the same machine (OS).

--Change the Python version --Switching libraries --You can take the created environment to another environment by using the virtualenv command.

That is overwhelmingly faster than building from scratch, and you can do it with just a few commands. I was surprised when I first touched it, but it is a very development-friendly function. Can you use docker? </ s>

virtualenv reference

http://cloverrose.hateblo.jp/entry/2012/10/01/235126 http://qiita.com/H-A-L/items/5d5a2ef73be8d140bdf3 http://qiita.com/Kodaira_/items/feadfef9add468e3a85b

sudo python doesn't work?

I usually use python with Anaconda on Windows and also virtualenv via the conda command, but there is no culture of requiring sudo on windows. Therefore, I completely forgot that there was something that required sudo.

At that time, I was trying out a server backend program for python called bottle.

Until bottle installation


$ virtualenv --no-site-packages bottle_test
$ cd bottle_test
$ pip install bottle

Then I copied the sample program to start_server.py and tried to run it.

test


$ python start_server.py
   ...(Omission)...
socket.error: [Errno 13] Permission denied

Oh, it's sudo. I forgot.

Run with sudo


$ sudo python start_server.py
   ...(Omission)...
ImportError: No module named bottle

that? Did you forget to put the bottle? Is that stupid?

Version confirmation


$ pip freeze
appdirs==1.4.3
bottle=0.12.13
packaging=16.8
pyparsing==2.2.0
six=1.10.0

Ah, that? It's installed ... why do I get an error? ,,,Ah!

Cause: There is no virtualenv available after promotion when sudo

sudo will temporarily switch your home directory to / root, and you haven't created a virtualenv in / root. so. What I just created is the logged-in user's own virtualenv, not for root. It was like that.

Correspondence? Correspondence.

After investigating, I found about three ways to deal with it.

  1. Call python in virtual environment directly
  2. Create a virtual environment with sudo
  3. Don't write code that requires sudo in the first place

Well, I can't use 3 for what I'm trying to do this time.

1. Call python in virtual environment directly

It depends greatly on the folder structure when the virtual environment was created, but for example, if you create it as follows

Creating a virtualenv


~ $ mkdir test
~ $ cd test
~/test $ virtualenv --no-site-packages testenv
~/test $ cd testenv
~/test/testenv $ source bin/activate

In that case, the python path used by the virtual environment testenv is ~ / test / testenv / bin / python.

You can call this directly with sudo to start python with the virtual environment testenv.

Execution method that works as expected


(testenv) $ sudo ~/test/testenv/bin/python start_server.py

2. Create a virtual environment with sudo

I haven't tried this method. The idea is simple: try to create a virtualenv that you can use when you sudo.

Create virtualenv with sudo


$ sudo virtualenv venv

In this case, the package installation seems to be as follows.

$ sudo venv/bin/pip $PACKAGE
$ sudo sh -c ". venv/bin/activate; pip install $PACKAGE"

sudo + virtualenv execution reference

https://askubuntu.com/questions/234758/how-to-use-a-python-virtualenv-with-sudo http://stackoverflow.com/questions/2658902/in-my-virtualenv-i-need-to-use-sudo-for-all-commands http://emilkirkegaard.dk/en/?p=5770

in conclusion

I shared the pitfall experience of python virtualenv. I hope it will be a reference for your development life! Eh, don't you use python? Is it javascript main? I'm usually C # main! </ s> Then!

Recommended Posts