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.
Related
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
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.
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.
I'm trying to install PyCaffe for Python 3.5.3 on AWS EC2 with Ubuntu 14.04 without Anaconda, following the installation instructions.
I successfully compiled it on the same machine for Python 2.7 but when compiling for version 3.5 I get the following error:
ubuntu#ip-172-31-3-227:~/caffe$ make pycaffe
CXX/LD -o python/caffe/_caffe.so python/caffe/_caffe.cpp
python/caffe/_caffe.cpp:1:52: fatal error: Python.h: No such file or directory
#include <Python.h> // NOLINT(build/include_alpha)
I saw this post and performed:
sudo apt-get install python3-dev
I found this, so I performed the equivalent command for Python 3.5:
for req in $(cat requirements.txt); do sudo pip3.5 install $req; done
All installations worked but it didn't fix the problem.
I many other posts on similar problems but not this exact problem (so if you find something please check that it's really the same situation before rushing to say I didn't look well enough).
P.S.
I also saw this post but I don't understand which directories to add to the path.
When you do a pip install package,when pip finds new version,it uninstall the current one(dateutil in your case) when the package was installed using certain permission,pip needs the same permission to uninstall/upgrade it.
A quick fix would be to change this
`for req in $(cat requirements.txt); do pip3.5 install $req; done` to
for req in $(cat requirements.txt); do sudo pip3.5 install $req; done
It seems you dont have permission to access /usr/local/lib/python3.5/ folder as user,the packages that goes in /usr/local are required by the system but only available to you and only you on the system, i assume since only only they are restricted to you and pip want to access assuming that no restriction is there,then it get permission denied ,so you might consider doing
for req in $(cat requirements.txt); do pip3.5 --user yourusername install $req; done
the alternative is to remove any restriction to execute and write on that folder
by doing chmod -R 777 /usr/local/lib/python3.5/ which is usually not recommended but the problem you need pip to be able to read,write,execute anything in that folder.
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