I have a shell script which runs to predict something on raspberrypi which has python version 2.7 as well as 3.5. To support audio features I have made python 3.5 as default. when I run the shell script it is taking version 2.7 and throwing an error.The error is shown in this link
3 Answers
It's not exactly clear based on the wording of your question how you have your scripts are set up. However, if you are calling python in the shell script, you can always specify python3 or python2 instead of just calling python (which points to your system's default). This would look something like this:
$ python3 python_script.py
If you are calling a script via the command line (e.g. something along the lines of ./python_script.py in shell) directly that contains python code in it, you can also format the header of the script like so to force a specific version of python:
#!/usr/bin/env python3
python source code here
1 Comment
I would suggest adding an alias to the root bashrc file as you seem to be calling this thing as the root user.
something to the effect of alias python=python3.5 at the bottom of the file ~root/.bashrc may have the effect your looking for although I'm sure there's a more permanent solution out there.
Comments
You can select version of existent python explicitly at the top of the script.
For example, I have a script which subexecutes script in 2.7.12 python and in 3.5.2 python. Let it be named: "different py versioned.sh"
Its code:
#! /bin/bash
./'py in default.py'
./'py in 3.5.py'
Code of 'py in 3.5.py':
#!/usr/bin/python3.5
import sys
print("Python version")
print (sys.version)
Code of 'py in default.py':
#!/usr/bin/python
import sys
print("Python version")
print (sys.version)
Then output of executing: ./'different py versioned.sh'
Python version
2.7.12 (default, Oct 8 2019, 14:14:10)
[GCC 5.4.0 20160609]
Python version
3.5.2 (default, Oct 8 2019, 13:06:37)
[GCC 5.4.0 20160609]
shows that every piece of code was executed in its separate py version.
/usr/local/bin/python3.