different result in find_executable(): python vs sudo python - python

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

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.

Related

Python 2.7, not Anaconda 3 running when I run as root using sudo [duplicate]

This question already has answers here:
How to set Python3.5.2 as default Python version on CentOS?
(6 answers)
Closed 3 years ago.
When I run python on RHEL, I automatically use Anaconda3:
Python 3.7.3 (default, Mar 27 2019, 22:11:17)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
When I sudo python, it defaults to python 2.7.
Python 2.7.5 (default, Sep 12 2018, 05:31:16)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
"which python" vs "sudo which python" gives:
/opt/anaconda3/bin/python
/bin/python
How can I make sudo commands run Anaconda distribution of python. Is there any risk of changing this?
Can I make the change permanently, or do I just run python from sudo using the full Anaconda path?
Your python version and installation location for root is different. If you want to use /opt/anaconda3/bin/python , there are different ways:
You can add alias python="/opt/anaconda3/bin/python" to your .bashrcfile of root user and re login or source this .bashrc.
Other way is to use #!/opt/anaconda3/bin/python in your python code when you run it from root user so that is uses your anaconda distribution.
Point is, you have to use /opt/anaconda3/bin/python as your python binary.
You mayalso remove python2.7 from your root user and add /opt/anaconda3/bin/python in your PATH env variable.
Also, you can add /opt/anaconda3/bin/python in your PATH environment variable and use python3 instead of python from root user. or you can use /opt/anaconda3/bin/python instead of python
Make sure the permissions and owner ship of paths are good without conflicts among users.

In Python from Cygwin, os.symlink not creating proper Symlink

Based on below code, I am trying to create symlink from Cygwin command prompt. It generates a symlink file but it seems not valid windows symlink.
Below code works fine and generates proper symlink if I run the same code from native command prompt.
$ python3
Python 3.6.8 (default, Feb 14 2019, 22:09:48)
[GCC 7.4.0] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.symlink("package.jpg", "sample.jpg")
>>>

How to make python to choose a specific version of opencv?

I am using ubuntu14.04 and python2.7 and I have installed opencv3.2.0 (/usr/local) and opencv2.4.8(/usr/local/opencv/2.4.8) in my machine. The outcome of command
pkg-config --modversion opencv
is 2.4.8
while python script
print cv2.__version__
is 3.2.0.
What should I do to change it to 2.4.8?
=========================================================================
I have tried export PYTHONPATH=/usr/local/opencv/2.4.8/:$PYTHONPATH
It seems no use
$ export PYTHONPATH=/usr/local/opencv/2.4.8/:$PYTHONPATH
$ python
Python 2.7.6 (default, Oct 26 2016, 20:30:19)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> print cv2.__version__
3.2.0-dev
>>>
Although It shows the version is 3.2.0, it is actually using the 2.4.8, thanks for IronFarm's answer
Add the directory for the v2.4.8 to the beginning of your PYTHONPATH environment variable before running Python.
On Linux:
export PYTHONPATH=/usr/local/opencv/2.4.8/:$PYTHONPATH

pip prints trash messages in stdout

When I try to use pip commands, annoying messages are coming out in stdout:
~# pip -V
Platform: linu
pip 1.5.4 from /usr/lib/python2.7/dist-packages (python 2.7)
~# pip install
Platform: linu
You must give at least one requirement to install (see "pip help install")
Python commands are working normally.
OS - Ubuntu 14.04
I tried to reinstall pip and all dependencies, but it didn't help.
What is that and where it comes from?
The problem was noticed when I tried to use ec2.py dynamic inventory script for AWS. I faced the same problem as here:
https://github.com/ansible/ansible/issues/14667
ec2.py generates JSON with starting "Platform: linu" and therefore ansible doesn't work with that.
Also I searched for boto library (used in ec2.py) and pip configs. But they are blank.
Any suggestions?
Python
~# python
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> print os.name
posix
>>> import platform
>>> platform.system()
'Linux'
Found one more way to reproduce the issue:
:/usr/bin# python
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> import os
>>> import boto
Platform: linu
>>>
This is common mistake. Most of this weird problem occur (whether in a plain PC, VM or inside cloud instance such as EC2. Did you notice AWS EC2 using a different locked down pip version) is running Python in sudo mode.
DO NOT run PIP in sudo mode!
My suggestion : setup Virtualenv and install your package on that virtualenv. Then use mkvirtualenv yourenv to create an custom virtualenv.
To load a python script automatically with the designated virtualenv, you just need to put an extra line inside your bash script to trigger your python package/modules.
source <virtualenv_folder>/<virtualenv_name>/bin/activate

How do I add tab completion to the Python shell?

When starting a django application using python manage.py shell, I get an InteractiveConsole shell - I can use tab completion, etc.
Python 2.5.1 (r251:54863, Apr 15 2008, 22:57:26)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
When just starting a python interpreter using python, it doesn't offer tab completion.
Can someone tell me what django is doing to give me an interactive console, or what I need to do to start an interactive console without a django app?
I may have found a way to do it.
Create a file .pythonrc
# ~/.pythonrc
# enable syntax completion
try:
import readline
except ImportError:
print("Module readline not available.")
else:
import rlcompleter
readline.parse_and_bind("tab: complete")
then in your .bashrc file, add
export PYTHONSTARTUP=~/.pythonrc
That seems to work.
I think django does something like https://docs.python.org/library/rlcompleter.html
If you want to have a really good interactive interpreter have a look at
IPython.
For the record, this is covered in the tutorial: http://docs.python.org/tutorial/interactive.html
I use ptpython - it is a wonderful tool autocomplete shell cmd.
Installing ptpython is very easy, use pip tool
pip install ptpython
and for django shell, you should import the django env, like this
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "testweb.settings")
Trust me, this is the best way for you!!!
Fix for Windows 10 shell:
pip install pyreadline3 # previously, pyreadline but that package was abandoned
pip install ipython
It looks like python3 has it out-of box!
In Python3 this feature is enabled by default. My system didn't have the module readline installed. I am on Manjaro. I didn't face this tab completion issue on other linux distributions (elementary, ubuntu, mint).
After pip installing the module, while importing, it was throwing the following error-
ImportError: libncursesw.so.5: cannot open shared object file: No such file or directory
To solve this, I ran-
cd /usr/lib
ln -s libncursesw.so libncursesw.so.5
This resolved the import error. And, it also brought the tab completion in the python repl without any creation/changes of .pythonrc and .bashrc.
Yes. It's built in to 3.6.
fernanr#gnuruwi ~ $ python3.6
Python 3.6.3 (default, Apr 10 2019, 14:37:36)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.
Display all 318 possibilities? (y or n)
os.CLD_CONTINUED os.O_RDONLY os.ST_NOEXEC os.environ os.getpid( os.readlink( os.spawnvpe(
os.CLD_DUMPED os.O_RDWR os.ST_NOSUID os.environb os.getppid( os.readv( os.st
For older versions (2.x) above script works like charm :)
fernanr#crsatx4 ~ $ cat .bashrc | grep -i python
#Tab completion for python shell
export PYTHONSTARTUP=~/.pythonrc
fernanr#crsatx4 ~ $ . ~/.bashrc
fernanr#crsatx4 ~ $ echo $?
0
fernanr#crsatx4 ~ $ python2
Python 2.7.5 (default, Jun 11 2019, 14:33:56)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.
Display all 249 possibilities? (y or n)
os.EX_CANTCREAT os.O_WRONLY

Categories