1

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?

DYZ
  • 55,249
  • 10
  • 64
  • 93
Gopinath S
  • 511
  • 6
  • 20
  • What does it mean when you say you're "loading" python2? – Scott Skiles Aug 23 '18 at 20:50
  • This may help you, @Gopinath: https://stackoverflow.com/questions/20842732/libpython2-7-so-1-0-cannot-open-shared-object-file-no-such-file-or-directory – Scott Skiles Aug 23 '18 at 20:51
  • @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 Aug 24 '18 at 16:41
  • 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 Aug 24 '18 at 19:46

1 Answers1

0

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!")?

Scott Skiles
  • 3,647
  • 6
  • 40
  • 64
  • @DYZ - I'd appreciate the removal of the downvote if deemed a sufficient answer. – Scott Skiles Aug 23 '18 at 20:43
  • 2
    Your answer does not address the question whatsoever. The OP has two versions of Python installed in such a way that one interferes with the other. Clearly, in your example, it is not an issue, because you can run any version of python. You correctly mention virtual environments, but you do not explain, how to use them. – DYZ Aug 23 '18 at 20:45