How to get the python3 path in ubuntu without which command - linux

Problem:
I want to get the python3 path in my Ubuntu VM. Like /usr/xyz/python3
More Information:
I dont see the 'which' command present in my envt.
I tried to 'sudo apt install which', but it fails saying that no such package found.

Get path of the executable python binary
Python code
import sys
print(sys.executable)
Command
python -c "import sys; print(sys.executable)"

sudo apt install which is failing because which is part of debianutils; most executables don't come as their own package.
sudo apt install debianutils

If you use bash, you can just do :
type -p python

The where and which commands are often an alias to the inbuilt bash command type.
I have it also as app in busybox on most embedded devices.
Also tablets and smartfons with the app: ConnectBot
If present on your machine too; then: type -p python3
But anyway, the correct/recommended way to make a SHEBANG is not to use type, where or which.
If your PATH environment variable targets the usually folders for installed executables just use: #!/usr/bin/env python3
Example: test.py
#!/usr/bin/env python3
import sys
print('No error - But i return 1 and not 0')
sys.exit(1)
Making than executable with: chmod +x test.py
And execute it...
$ ./test.py
No error - But i return 1 and not 0

Related

How to change python3 to default in linux mint

I have a little problem with changing python3 to default in newly installed operating system linux Mint 19.3. It was pretty easy on Ubuntu 16.04 but now I need small help here.
So, I run
python --version
and got this
Python 2.7.15+
Than I run
python3 --version
and this was the result
Python 3.6.8
After entering this command
sudo update-alternatives --config python
I received obvious info
update-alternatives: error: no alternatives for python
Both version of python are located in /usr/bin folder.
The issue occurs when I'm trying to change python3 to as a default by typing the command
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.6
The terminal outputs the following
update-alternatives: --install needs <link> <name> <path> <priority>
Any help here would be welcome.
In your case, priority is missing, just append 1 at the end of the command like this :
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.6 1
To define python3 as your default version of python on linux, you need to add a specific line to .bashrc file located in your home directory.
Here is a simple method to do so through command line interface (termianl).
Run:
$ echo "alias python='python3'" >> .bashrc
$ source .bashrc
then run python --version to check if the change has applied.
#darkfidis answer is correct. But you may also need to run to set or change the default python version.
sudo update-alternatives --config python

mprof run <executable> gets me "Command not found"

I am using Ubuntu 18.04 and Python 3. I have installed memory_profiler using the following command:
pip3 install -U memory_profiler
I was able to run python3 -m memory_profiler <executable> from the command line with no issues. However, if I try mprof run <executable> I get the following output:
Command 'mprof' not found, did you mean:
command 'gprof' from deb binutils
command 'pprof' from deb tau
command 'mlprof' from deb mlton-tools
command 'sprof' from deb libc-dev-bin
command 'prof' from deb profphd
Try: sudo apt install <deb name>
I am following the steps in the documentation of memory_profiler here but it's not working. Is there any extra step given the fact that I use Python 3?
One way to make it work is the following. Instead of mprof run <executable>, the following works:
python3 /home/myuser/.local/lib/python3.6/site-packages/mprof.py run <executable>
You must replace myuser with the appropriate value. Your full path may differ. You need to find where is mprof.py. The general command is: python3 /full/path/to/mprof.py run <executable>.

ImportError: No module named pyaudio with sudo

I am trying to run a python3 script using sudo on raspberry pi, but it is always giving me an import error the first line, in my case "module pyaudio is not found".
I have tried changing the sudoers file and putting in an env_keep += "my_python_path" like this post : PYTHONPATH not working for sudo on GNU/Linux (works for root)
but it doesn't seem to work.
However, when I run sudo with python2 or when I run python3 without sudo the import errors don't happen. Any suggestions on how to get it work with python3?
I just used sudo su to gain root access and everything worked fine.

sudo or not sudo? [duplicate]

This question already has answers here:
How to install python modules without root access?
(9 answers)
Closed 4 years ago.
The ultimate goal is to run this single command from inside a shell script on a redhat machine. I've used the script for years on an ubuntu machine, but i have fewer privileges on redhat. I'll describe my attempted solution below, but wanted to frame the question first.
read -r val1 val2 val3 <<<$(python3 script_name.py "$json_args")
In redhat, i had to install python/pip3.5 as sudo... like this...
sudo yum -y install https://centos7.iuscommunity.org/ius-release.rpm
sudo yum -y install python35u
sudo yum -y install python35u-pip
sudo pip3.5 install --upgrade pip
sudo pip3.5 install boto3
sudo pip3.5 install awscli --upgrade --user
different machines may have different python versions, so I created an alias for python3 in .bash_profile so the same shell script works everywhere.
echo 'alias python3="python3.5"' >>~/.bash_profile
now... everything was locked down in python... I could import boto3, but it wasn't usable... from python3 command line to demonstrate...
>>> import boto3
>>> boto3.__version__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'boto3' has no attribute '__version__'
so I tried running python3 as 'sudo', but got...
sudo: python3: command not found
so I added an alias to .bashrc like this...
echo 'alias sudo="sudo "' >>~/.bashrc
Great! now things seem to work! i can finally run the command i originally wanted (see below).
sudo python3 script_name.py args
or more specifically (note the added sudo compared to my intro)...
read -r val1 val2 val3 <<<$(sudo python3 script_name.py "$json_args")
working great from the command line!
... until I try putting it in a shell script. now I'm back to my original error...
sudo: python3: command not found
I've tried all kinds of things... putting the 'sudo ' alias in the script... putting it in /root/.bashrc... a few other random things.
At this point I suspect i could run the bash script as sudo too... but that starts to cause all kinds of other problems and I suspect is a pretty terrible security practice. I feel like I've gone off the rails and there's some much smarter solution here.
any ideas for either
how to run python3.5 without sudo and get it to work
how to get the sudo python3 to work inside the shell script without running the shell script as sudo?
thanks in advance
EDIT
based on suggestions below from #JulianLoaiza and #TerryCarmen, the chown -R allows me to run python3 without the sudo... but boto3 can't authorize me anymore when I do. Checking sys.path there's only one difference from python's perspective... that's
sudo has: '/root/.local/lib/python3.5/site-packages'
w/out sudo has: '/home/ec2-user/.local/lib/python3.5/site-packages'
both have '/usr/lib/python3.5/site-packages', which appears last (and contains the libs I explicitly installed).
what might be happening... /root/.local/lib/python3.5/site-packages has nothing in it about awscli or boto... /home/ec2-user/.local/lib/python3.5/site-packages DOES have 'awscli' and 'botocore' stuff in it. So does /usr/lib/python3.5/site-packages... which also has boto3 and other libs I explicitly intalled.
Could python be getting confused by looking in /home/ec2-user/.local/lib/python3.5/site-packages before /usr/lib/python3.5/site-packages when I'm not logged in as 'sudo'?
You can try change the folder ownership,
sudo chown -R ec2-user:ec2-user /usr/lib/python2.7
sudo chown -R ec2-user:ec2-user /usr/lib64/python2.7
sudo: python3: command not found
It can't find your version of python.
You'll need to specify the full path to the python binary:
read -r val1 val2 val3 <<<$(sudo /location/of/your_python script_name.py "$json_args")
Once you have it running, if python can't find it's dependencies, you can add
import sys
sys.path.append('/location/of/your_python_libs')
to your python code
As for sudo, your options are to fix the code and directories and users so it doesn't need root, or keep using sudo. It's not a good practice, but at the end of the day they're your system(s) and your decision.

Yum crashed with Keyboard Interrupt error

I installed the newer version of python (3.2.3) than the one available in Fedora16 (python2.7)
And now yum stops working. It shows the following error.
[root#localhost yum-3.4.3]# yum
File "/usr/bin/yum", line 30
except KeyboardInterrupt, e:
^
SyntaxError: invalid syntax
Please advice as how to resolve the error. It would be helpful as I am not able to update or install any package.
Because yum does not support Python3.
You can run command vi /usr/bin/yum, change /usr/bin/python to /usr/bin/python2 in first line.
Thus you can run the command yum by Python2 instead of Python3.
Note however that this will make your setup unsupported and thus unmaintainable (as does what you did). You will likely have other similar problems in the future with other system packages.
If you want to use an alternative Python installation, consider installing it into /usr/local, /opt or using pyenv.
This issue happens when user upgrades to python3, Just simply edit the file --> /usr/bin/yum and change to first line to --> "#!/usr/bin/python2"
The above solution wouldn't solve the all yum dependency problems, its better to run the below commands.
sudo ln -s /usr/local/bin/python3 /usr/bin/python3 (Mark latest python as python3)
sudo ln -sf /usr/bin/python2.7 /usr/bin/python (nake 2.7 as default python)
THanks,
Daman
I'm guessing you installed Python 3.2.3 from source and used "make install" as the last command. That command has the unfortunate side-effect of replacing the system installed version command "python" with the new version. Using "make altinstall" doesn't replace "python".
The command "python" is just a symbolic link to "python2", which in turn is a symbolic link to "python2.7". You should be able to restore "python" by executing the following command:
cd /usr/bin
sudo ln -s python2 python
Thanks Damanvir! Changing the line in /usr/bin/yum worked!
This is a little off topic and might be removed but it might help someone.
These are the steps I used to install Python 3.7 on Centos and fix the yum error.
Download from https://www.python.org/
tar -xvf
./configure --enable-optimizations
make
make install
OR
make altinstall
make altinstall is used to prevent replacing the default python binary file /usr/bin/python.
cd /usr/bin
Remove the current symbolic link to the previous version
rm python
OUTPUT: rm: remove symbolic link ‘python’? y
Find the location of the new version
whereis python3.7
OUTPUT: python3: /usr/local/bin/python3.7
Verify this is correct
/usr/local/bin/python3.7 --version
OUTPUT: Python 3.7.0
Create a symbolic link to the location of the new version
ln -s /usr/local/bin/python3.7 python
python --version
OUTPUT: Python 3.7.0
Yum commands will show the following error:
File "/bin/yum", line 30
except KeyboardInterrupt, e:
SyntaxError: invalid syntax
Change the top line of this file from using python to python2
vi /usr/bin/yum
#!/usr/bin/python2
Reference: https://tecadmin.net/install-python-3-7-on-centos/
The real answer - to ensure that you are back on a supportable version of python (in the event you are looking at this for an issue with RHEL 7).
cd /usr/bin
sudo unlink python
sudo ln -s python2 python
Your yum looks for python2. Let's use 'alternatives' to switch between pythons2 and python3.
run --> sudo alternatives --config python
Enter to keep the current selection[+], or type selection number:
If you don't configure it. How to do that?
sudo alternatives --install /usr/bin/python python
/usr/local/bin/python3.8 60
sudo alternatives --install
/usr/bin/python python /usr/bin/python2 50
It's only a binary link issue. You can copy the working /usr/bin/python2 from any system and copy to current system location /usr/bin/python2
#cd /usr/bin
#ln -s python2 python

Resources