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.
Related
I'm trying to get Tf2onnx to run so that I can convert a saved_model.pb file to onnx format. Nothing is really working out. I have tried installing it with the pip install git+https://github.com/onnx/tensorflow-onnx command but same issue.
The command should be issued from the site-packages directory, not the tf2onnx directory. I also put a sudo in front which helped.
sudo python3 -m tf2onnx.convert --saved-model /home/isaacpadberg/Desktop/model --output model.onnx
I want to install matlab in ubuntu 20.04, but I'm running ubuntu in WSL2 and it doesn't want to work.
I keep getting the error: terminate called after throwing an instance of 'framework::window::DisplayError what(): No. Display Avaliable.
I have the linux version of matlab unzipped in a folwer and I'm trying to instal it into the user files. Things I've tried to install it that were suggested for people having the same issue on other distros:
sudo ./install
./install
bash install
install
bash ./install
sudo bash ./install
sudo su and then doing ./install
export DISPLAY=:0.0 and then sudo ./install
bash ./install -v -inputFile installer_input.txt
It gives the same error for every one that I've tried.
Let me know if anyone has any solutions. Thanks.
Posting this to help out others in the future. It actually involved a few things to get this fully working properly:
1)Had to get a X Server
2)Had to change display settings in ubuntu to get it to recognize the X server and turn off some firewall features for Windows.
3)Had to when installing matlab install using the legacy install file instead of the normal install file
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
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.
I followed installment instructions mentioned here
Its a simple pip install command
After that I went to my linux terminal and wrote in2csvbut got the following error:
/usr/bin/in2csv: No such file or directory
Originally I tried to install it using the command:
sudo apt-get install python3-csvkit
And the in2csv command used to work on terminal, but it appears to work under python 3 installation and I need it under my python2.7.
so I uninstalled the python3-csvkit, and installed it again using pip install, but again its not working from terminal, this way.
Any ideas why, and how to solve it?
in2csv command manual: here
I just had this problem and solved with:
sudo pip install csvkit
Without the sudo pip installs csvkit in /home/username/.local/lib/python2.7/site-packages and probably puts the executable in /home/username/.local/bin. You could avoid the sudo by adding that to your shell PATH.