Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PYTHONPATH not working for sudo on GNU/Linux (works for root)

Tags:

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 
like image 302
usr Avatar asked Nov 01 '11 16:11

usr


People also ask

How do I set Pythonpath environment variables in Ubuntu?

Setting path at Unix/Linux In the csh shell − type setenv PATH "$PATH:/usr/local/bin/python" and press Enter. In the bash shell (Linux) − type export PATH="$PATH:/usr/local/bin/python" and press Enter.

How add Pythonpath Linux?

If you are using the standard flavour of Linux, open up the bash shell and type the following phrase, export PATH=”$PATH:/usr/local/bin/python” and press Enter. If you have access to either sh or ksh shell, then open up the terminal and type the following, PATH=”$PATH:/usr/local/bin/python” and press Enter.


1 Answers

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
like image 96
usr Avatar answered Sep 17 '22 20:09

usr