0

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'?

kmh
  • 1,516
  • 17
  • 33
  • The `"sudo "` alias you're describing should never be necessary on any system. Something is seriously wrong here. –  Aug 14 '18 at 23:20
  • since bash only checks the first word to see if it's an alias... and for me, 'python3' is an alias... how else would you run `sudo python3` without making sudo an alias also? saw this kind of workaround here: https://askubuntu.com/questions/22037/aliases-not-available-when-using-sudo – kmh Aug 15 '18 at 00:50
  • 1
    Possible duplicate of [How to install python modules without root access?](https://stackoverflow.com/q/7465445/608639), [Install python package without root access](https://stackoverflow.com/q/20585218/608639), [Installing pip locally without root privileges](https://stackoverflow.com/q/40852267/608639), [Why i can't do some things without sudo using Python and pip?](https://stackoverflow.com/q/33922240/608639), etc. – jww Aug 15 '18 at 01:12
  • these links are not solutions to my problem. i know how to install with pip to the local-user space to avoid using sudo. that was never the issue. the issue is that when installed that way, the library doesn't work when the python script is called from a shell script. – kmh Aug 16 '18 at 18:13

2 Answers2

1

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
jww
  • 97,681
  • 90
  • 411
  • 885
  • ooh... this is helpful... i did `chown -R` on python3.5 like this... and I can now run python3 (the alias for python3.5 in this case) without sudo, but boto3 inside the python script gives me `UnauthorizedOperation` errors if I do. If I run python3 as sudo... boto3 works as desired, but I can't run the alias python3 as sudo from a shell script, only the command line. – kmh Aug 15 '18 at 00:59
1
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.

Terry Carmen
  • 3,720
  • 1
  • 16
  • 32
  • for my use, this needs to run in multiple environments on multiple linux distributions and multiple versions of python3. it needs to be a common alias that works in all environments. – kmh Aug 15 '18 at 00:55
  • Your only choices are to install python so it's in the path, change the path so python can be found, or specify the full path to the executable as shown above. – Terry Carmen Aug 15 '18 at 02:10