Doing everything in one heredoc in the SCL environment is the best option, IMO:

scl enable python27 - << \EOF
cd /var/www/python/scripts/
python runAllUpserts.py >/dev/null 2>&1
EOF

Another way is to run just the second command (which is the only one that uses Python) in scl environment directly:

cd /var/www/python/scripts/
scl enable python27 "python runAllUpserts.py >/dev/null 2>&1"
Answer from slavek on Stack Overflow
🌐
Red Hat
access.redhat.com › solutions › 1252913
How to use Red Hat Software Collections in scripts and cron jobs - Red Hat Customer Portal
Adjust the script header to use #!/usr/bin/env python as the scripts interpreter definition and setup cron, enabling Red Hat Software Collections 30 * * * * scl enable python27 <path to script>/python_script.py
🌐
Red Hat
docs.redhat.com › en › documentation › red_hat_software_collections › 1 › html › packaging_guide › sect-extending_the_python27_and_python33_software_collections
4.2. Extending the python27 and python33 Software Collections | Packaging Guide | Red Hat Software Collections | 1 | Red Hat Documentation
%package build Summary: Package shipping basic build configuration Requires: scl-utils-build # Require python27-scldevel (or python33-scldevel) so that there is always access # to the %%scl_python and %%scl_prefix_python macros in builds for this Software # Collection Requires: %{scl_prefix_python}scldevel �scription build Package shipping essential configuration macros to build %scl Software Collection. %prep %setup -c -T %install %scl_install # Create the enable scriptlet that: # - Adds an additional load path for the Python interpreter.
Top answer
1 of 1
2

You should verify that python27 is coming from SCL and not elsewhere.

In my case I am using CentOS 6, but the process is the same.

So:

$ yum info python27  
Loaded plugins: fastestmirror, priorities
Loading mirror speeds from cached hostfile
6 packages excluded due to repository priority protections
Available Packages
Name        : python27
Arch        : x86_64
Version     : 1.1
Release     : 25.el6
Size        : 5.2 k
Repo        : centos-sclo-rh
Summary     : Package that installs python27
License     : GPLv2+
Description : This is the main package for python27 Software Collection.

We can see it comes from centos-sclo-rh and so is the right version.

We can install this. Because it's from SCL it will install into /opt/rh and this will not impact any other aspect of the OS:

$ sudo yum install python27
...
$ ls /opt/rh
python27

We can see the default python is still unchanged:

$ /usr/bin/python --version
Python 2.6.6

Now we need the scl command. This is from the scl-utils package, which you may need to install (yum install scl-utils).

$ scl enable python27 bash

This runs a new shell with the path changed:

$ scl enable python27 bash
bash-4.1$ echo $PATH
/opt/rh/python27/root/usr/bin:/usr/local/bin:/usr/bin/X11:/etc:/usr/local/sbin:/sbin:/usr/sbin
bash-4.1$ command -v python
/opt/rh/python27/root/usr/bin/python
bash-4.1$ python --version
Python 2.7.8

So enabling and running SCL does not impact the core OS; it won't break anything you normally run but allows for a newer version of python to be installed in parallel (in /opt/rh).

🌐
Drexel
proteusmaster.urcf.drexel.edu › urcfwiki › index.php › Red_Hat_Software_Collections
Red Hat Software Collections - URCFwiki
[juser@proteusi01]$ scl -l postgresql92 python27 python33 · To select and use Python 2.7 interactively: [juser@proteusi01]$ python --version Python 2.6.6 [juser@proteusi01]$ scl enable python27 bash # starts a new bash, with python27 enabled [juser@proteusi01]$ python --version Python 2.7.5 ...
🌐
GitHub
gist.github.com › dalegaspi › dec44117fa5e7597a559
Installing Python 2.7 on CentOS 6.5 · GitHub
sudo yum update # update yum sudo yum install centos-release-scl # install SCL sudo yum install python27 # install Python 2.7 · To use it, you essentially spawn another shell (or script) while enabling the newer version of Python:
🌐
Stack Exchange
unix.stackexchange.com › questions › 290213 › enable-scl-python27-so-other-rpms-can-see-it
rhel - Enable SCL python27 so other RPMs can see it - Unix & Linux Stack Exchange
June 16, 2016 - rpmbuild adds Python requiremenst automatically from the version of python used. As such, it was adding python(abi)=2.7 to the requires list of the RPM. However, when using the python27 that scl was using provides python27-python.
Find elsewhere
Top answer
1 of 3
3

You should try using -- instead of surrounding your command with quotes.

scl enable python27 -- ls /tmp

I was able to make a python script that uses the rh-python35 collection with this shebang:

#!/usr/bin/scl enable rh-python35 -- python

import sys
print(sys.version)
2 of 3
1

The parsing of arguments in the she-bang command is not really defined. From man execve:

The semantics of the optional-arg argument of an interpreter script vary across implementations. On Linux, the entire string following the interpreter name is passed as a single argument to the interpreter, and this string can include white space. However, behavior differs on some other systems. Some systems use the first white space to terminate optional-arg. On some systems, an interpreter script can have multiple arguments, and white spaces in optional-arg are used to delimit the arguments.

No matter what, argument splitting based on quote sis not supported. So when you write:

#!/usr/bin/scl enable python27 "ls /tmp"

It's very possible that what gets invoked is (using bash notation):

'/usr/bin/scl' 'enable' 'python27' '"ls' '/tmp"'

This is probably why it tries to open the "ls file at /etc/scl/prefixes/"ls

But it is just as likely that the shebang evaluates to:

'/usr/bin/scl' 'enable python27 "ls /tmp"'

And that would fail since it wont be able to find a command named enable python27 "ls /tmp" for scl to execute.

There's a few workarounds you can use.

You can call your script via scl:

$ cat myscript
#!/bin/bash
echo hello

$ scl enable python27 ./myscript
hello

You can also use the heredoc notation, but it might lead to subtle issues. I personally avoid this:

$ cat ./myscript
#!/bin/bash
scl enable python27 -- <<EOF
echo hi
echo \$X_SCLS
EOF

$ bash -x myscript 
+ scl enable python27 --
hi
python27

You can see one of the gotcha's already: I had to write \$X_SCLS to access the environment variable instead of just $X_SCL.

Edit: Another option is two have two scripts. One that has the actual code, and the second that simply does scl enable python27 $FIRST_SCRIPT. Then you wont have to remember to enter scl ... manually.

🌐
Medium
medium.com › @graghav › installing-python-2-7-on-centos-rhel-6-x-version-54e8404c9eb1
Installing Python 2.7 on CENTOS/RHEL 6.x version - graghav - Medium
October 31, 2020 - sudo yum install scl-utils sudo yum install centos-release-scl-rh sudo yum install python27 sudo scl enable python27 bash # Verify the update: python -V Python 2.7.8
🌐
Red Hat
docs.redhat.com › en › documentation › red_hat_software_collections › 2 › html › packaging_guide › sect-extending_the_python27_and_rh-python35_software_collections
4.2. Extending the python27 and rh-python35 Software Collections | Packaging Guide | Red Hat Software Collections | 2 | Red Hat Documentation
May 14, 2018 - These will be used when python27-scldevel (or rh-python35-scldevel) is not in the build root Only for this build, you need to override default __os_install_post, because the default one would find /opt/.../lib/python2.7/ and try to bytecompile with the system /usr/bin/python2.7 Similarly, override __python_requires for automatic dependency generator The directory for site packages for this Software Collection Always make sure that there is the python27-sclbuild (or rh-python35-sclbuild) package in the build root Require python27-python-devel, you will need macros from that package Require python27-scldevel (or rh-python35-scldevel) so that there is always access to the %%scl_python and %%scl_prefix_python macros in builds for this Software Collection Create the enable scriptlet that: - Adds an additional load path for the Python interpreter.
🌐
Stack Exchange
unix.stackexchange.com › questions › 184310 › installing-gdal-python-package-inside-python27-software-collection
scl - Installing gdal python package inside Python27 Software Collection - Unix & Linux Stack Exchange
February 11, 2015 - # you might need to do a sudo -s if your virtualenv is owned by root. scl enable python27 bash cd $VIRTUENV_ROOT source bin/activate pip install --no-install gdal==1.9.1 cd build/gdal python setup.py build_ext \ --gdal-config=/usr/bin/gdal-config \ --include-dirs=/usr/include/gdal/ python setup.py install
🌐
Stack Overflow
stackoverflow.com › questions › 43945540 › enable-scl-python27-for-apache-cgi
python 2.7 - Enable scl python27 for Apache cgi - Stack Overflow
May 12, 2017 - I have a .cgi script written in Python that requires python 2.7 but CentOS 6 does not have it. I installed through scl. I created python27.sh under /etc/profile.d #!/bin/bash source /opt/rh/pyt...
🌐
Blogger
threecop1.blogspot.com › 2011 › 06 › cron-scl-enable-python27-bash.html
cron - scl enable python27 bash -
October 20, 2017 - scl enable python27 - << \eof cd /var/www/python/scripts/ python runallupserts.py >/dev/null 2>&1 eof
🌐
Red Hat
access.redhat.com › discussions › 702583
Red Hat Customer Portal - Access to 24x7 support and knowledge
October 7, 2016 - I am trying to write python script running on RH 6 using python 2.7 from Software Collections (SCL). Since I couldn't find any way to use shebang with SCL I am trying to use a workaround: run bash script which enables python27 SCL and run python script.
🌐
GitHub
gist.github.com › phrawzty › 5db4212b04a74137d992
python27 in CentOS via SCL on Socorro Vagrant · GitHub
python27 in CentOS via SCL on Socorro Vagrant. GitHub Gist: instantly share code, notes, and snippets.