27

EDIT: Works for root, sudo is the problem. Read below.

I have a directory with my own libraries, e.g. my Python libraries are located at /home/name/lib/py.
I've added this directory to Python's PATH for all users (including root) by adding the following line to /etc/bash.bashrc:

export PYTHONPATH=$PYTHONPATH:/home/name/lib/py

It works for all users (including root). But it doesn't work for sudo. Is there any way I can make sudo use /etc/bash.bashrc?

EDIT: More information:

I've added PYTHONPATH to sudoers file like so: Defaults env_keep += "HOME PYTHONPATH". It sitll doesn't work.

env | grep PYTHON:
    PYTHONDONTWRITEBYTECODE=1
    PYTHONPATH=/home/name/lib/py

sudo env | grep PYTHON:
    PYTHONDONTWRITEBYTECODE=1

sudo echo $PYTHONPATH:
    /home/name/lib/py
3
  • 1
    You might try over at Super User as well, thought I don't think this needs migrating. Commented Nov 1, 2011 at 18:08
  • 2
    Especially with your edit that the problem is with sudo and not anything Python specific, this would probably get more useful answers on SU as mentioned, or on unix.stackexchange.com. Commented Nov 1, 2011 at 18:19
  • @brc I realize that now. I've fixed it though -- see my answer bellow. Commented Nov 2, 2011 at 1:14

6 Answers 6

25

The same is true for the PATH variable, it's also not carried into the super user environment, even though you're passing the preserve environment flag -E.

I'm using this sudo command now without any other modifications:

sudo -HE env PATH=$PATH PYTHONPATH=$PYTHONPATH ./bin/myscript

Since it's an alternative approach that works (for me) I thought I'd share here.

Sign up to request clarification or add additional context in comments.

1 Comment

The -H option is related to the home of target user. I didn't need it for your example to work here. Thanks for the lead!
22

The fix in my case was to remove Defaults !env_reset from sudoers.

But, I had to keep Defaults env_keep += "PYTHONPATH" in sudoers.
I've actually added Defaults env_reset (which resets environment variables), but it still works because of env_keep.

It seems that env_keep and !env_reset conflict with eachother, but that's just a guess.


So, the whole process:

  1. add export PYTHONPATH=/your/custom/path to ~/.bashrc or /etc/bash.bashrc
  2. add PYTHONPATH to Defaults env_keep += "ENV1 ENV2 ..." in sudoers file
  3. remove Defaults !env_reset from sudoers file if present

1 Comment

This solution also works on OS X. Just add a line Defaults env_keep += "PYTHONPATH" in /etc/sudoers on OS X.
2

Alternatives to manipulating PYTHONPATH:

Comments

2

Another tip:

sudo echo $PYTHONPATH:
    /home/name/lib/py

It won't work. Shell will interpret it like this:

1) expand $PYTHONPATH from env variable for example: /usr/lib/python

2) execute "sudo echo /usr/lib/python"

Comments

0

This should probably be posted somewhere else. But sudo will not process the environment file by default. If you want to invoke that the -i flag should help you out. It will simulate that users initial login.

You may have to play around with where you're putting your variables too. http://linux.die.net/man/8/sudo

1 Comment

sudo -i also didn't work for me. I think something somehow conflicted with !env_reset option in sudoers file (which I think is actually identical to sudo -i). See my answer for full explanation.
0

Follow configuration helps me to run multiple python services in dedicated VENVs on one Centos host

  1. Export env variables to separate file, for example /etc/sysconfig/my-app
  2. Set EnvironmentFile option in service config

see code below:

-bash-4.2$ sudo vi /etc/sysconfig/my-app

PATH=/usr/local/my-app/env/bin:$PATH
LD_LIBRARY_PATH=/usr/local/my-app/env/lib:$LD_LIBRARY_PATH



-bash-4.2$ sudo vi /etc/systemd/system/my-app.service

[Unit]
Description=my-app daemon
After=network.target


[Service]
EnvironmentFile=/etc/sysconfig/my_app
User=app_user
Group=app_user
Type=simple
ExecStart=/usr/local/my-app/env/bin/python /usr/local/my-app/main.py
Restart=on-failure
RestartSec=5s
PrivateTmp=true

[Install]
WantedBy=multi-user.target

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.