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
10
  • Probably, your root user doesn't have the same $PATH as your user ? Commented May 3, 2021 at 7:31
  • @Nephanth added result of echo, but both seems identical... strange. Commented May 3, 2021 at 7:37
  • but find_executable("pyside2-uic") returns true as user, and false as root ? Have you tried using which (as user and as root) ? Commented May 3, 2021 at 7: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. Commented May 3, 2021 at 7:51
  • no I mean which pyside2-uic Commented May 3, 2021 at 9:17

1 Answer 1

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.

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

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.