1
votes

my current bash shell is loaded with python3 but occasionally I tend to run old python2 scripts and I get this error

python/2.7.10/bin/python: error while loading shared libraries: libpython2.7.so.1.0: cannot open shared object file: No such file or directory"

so every-time I had to load python2 back and forth. Is there a way to run python2 script in python3 environment by just changing any environmental variables?

1
What does it mean when you say you're "loading" python2? - Scott Skiles
This may help you, @Gopinath: stackoverflow.com/questions/20842732/… - Scott Skiles
@Scott Skiles. By loading I meant choosing one of the available versions of python in the cluster. Thanks for the link, I did try them but it didn't seem to work for me. - Gopinath S
What do you get when you run which python, which python2, which python3? Generally: (1) You should not be simultaneously using python2 and python3 at the same time. (2) Users should be specifying in the old python scripts which verison of python to use. If that is the case, you can install a virtualenv such as conda. (3) When you need to, you can quickly change from a python3 virtual env to a python2 virutal env. - Scott Skiles

1 Answers

0
votes

Are you open to virtual envs? Otherwise you can run python2 or python3 from the command line.

As an example, create the following file, hello_world.py:

#!/usr/bin/env python
print "Hello, World!"

Then you can run from the bash shell:

python2 hello_world.py

That should work no problem. However, if you run:

python3 hello_world.py

You will run into the error:

SyntaxError: Missing parentheses in call to 'print'. Did you mean print("Hello, world!")?