简体   繁体   中英

Running python2 script in python3 loaded unix environment

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM