0

I'm writing this to ask what is the difference between executing python as normal user and previleged (i.e, sudo) user.

I have a python script that installs python package in specific directory (here, /usr/local is used), and in order to do that the script should be run with sudo.

The script seems calling external binary, however in sudo mode it fails to find it using find_executable(~), whereas it succeeds flawlessly without sudo command.

Here's code: calling script with & without sudo, respectively. Both codes have (nearly but impactless) identical contents.

Note that both python is identical, as I called it explicitly in sudo mode (I found that without specifying python binary path executes system-wide python).

w/ sudo:

sudo /home/.../anaconda3/envs/pytorch_open3d/bin/python
Python 3.8.8 (default, Feb 24 2021, 21:46:12) 
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from distutils.spawn import find_executable
>>> pyside2Uic = ["pyside2-uic", "python2-pyside2-uic", "pyside2-uic-2.7"]
>>> found_pyside2Uic = any([find_executable(p) for p in pyside2Uic])
>>> print(found_pyside2Uic)
False

w/o sudo:

which python
/home/.../anaconda3/envs/pytorch_open3d/bin/python

python
Python 3.8.8 (default, Feb 24 2021, 21:46:12) 
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from distutils.spawn import find_executable
>>> pyside2Uic = ["pyside2-uic"]
>>> found_pyside2Uic = any([find_executable(p) for p in pyside2Uic])
>>> print(found_pyside2Uic)
True

I also tried answer already provided (link), which is an argument that preserves current environment information, but has no effect:

sudo -E /home/.../anaconda3/envs/pytorch_open3d/bin/python
Python 3.8.8 (default, Feb 24 2021, 21:46:12) 
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from distutils.spawn import find_executable
>>> pyside2Uic = ["pyside2-uic"]
>>> found_pyside2Uic = any([find_executable(p) for p in pyside2Uic])
>>> print(found_pyside2Uic)
False

Is there anything I missed? Any helps are greatly appreciated. Thanks in advance.

ps. result of echo

echo $PATH
/home/.../anaconda3/envs/pytorch_open3d/bin:/home/.../anaconda3/condabin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
sudo echo $PATH
[sudo] password for ...: 
/home/.../anaconda3/envs/pytorch_open3d/bin:/home/.../anaconda3/condabin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
deugi
  • 11
  • 2
  • Probably, your root user doesn't have the same $PATH as your user ? – tbrugere May 03 '21 at 07:31
  • @Nephanth added result of echo, but both seems identical... strange. – deugi May 03 '21 at 07:37
  • but `find_executable("pyside2-uic")` returns true as user, and false as root ? Have you tried using which (as user and as root) ? – tbrugere May 03 '21 at 07:43
  • @Nephanth Yes I tried. 'which' gives different python path (conda's python in normal user, system-wide python in sudo), but I think this is negligible as I specified python binary to use conda's one in sudo mode. – deugi May 03 '21 at 07:51
  • no I mean `which pyside2-uic` – tbrugere May 03 '21 at 09:17
  • and your way of determining path for root is wrong : basically the $PATH variable is expanded before sudo is called (by the user path), so it will give you your user's path – tbrugere May 03 '21 at 09:20
  • try `sudo printenv PATH` – tbrugere May 03 '21 at 09:22
  • @Nephanth normal `which pyside2-uic` gives path to pyside2-uic in conda env, but `sudo which pyside2-uic` gives nothing – deugi May 04 '21 at 02:27
  • @Nephanth `printenv PATH` and `sudo printenv PATH` also gives different result, but `sudo printenv PATH` does not have path to conda python – deugi May 04 '21 at 02:28
  • `sudo env "PATH=$PATH" python` seems preserving user path. Thanks for discussion! – deugi May 04 '21 at 02:55

1 Answers1

1

Thanks to @Nephanth for valuable discussion,

I've found that there's a mismatch between result of which pyside2-uic and sudo which pyside2-uic, latter gives no path to the binary.

So I searched to related problems, and found link. From the answer,

sudo env "PATH=$PATH" python

preserved path of normal user.

deugi
  • 11
  • 2