简体   繁体   中英

Python's shebang evaluates to different Python version than the same path used in the console

Today is my first day with Python and I am using pyenv to deal with Python's versions. I successfully installed the latest version (3.6.3) and set it as global with

pyenv global 3.6.3

Now when executing in terminal

/usr/bin/env python

I get the expected correct response

Python 3.6.3 (default, Oct 24 2017, 02:48:04) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.

However the problem I have is that shebang I use in my script file has the same /usr/bin/env python path but who knows why it still evaluates to the preinstalled system's version of Python which is 2.7.6.

I am checking the version used by script like this

#!/usr/bin/env python

import sys
print("Content-type:text/html\r\n\r\n")
print(sys.version)

and what I get printed by the browser (using Apache2) is

2.7.6 (default, Oct 26 2016, 20:30:19) [GCC 4.8.4]

I was restarting apache, playing with pyenv configs, googling for any hints but no success.

Anybody has any idea what is wrong here? Thanks a lot!

pyenv allows you to set the used Python version on a per user basis .

So if you set it to 3.6.3 for the user misza , other users (like www-data ) will not be affected.

The webserver will most likely run as a special user (usually www-data ), so any changes you do to your personal settings will have no effect on whatever apache sees.

Having said that, I really think it is a bad idea to use pyenv on something like a server in any case: your script might not be the only one that uses a #!/usr/bin/env python shebang. It might also not be the only one that relies on being called by a specific Python version (and the odds have it, that most scripts with #!/usr/bin/env python are actually Python2.7 scripts).

So if you really want your script to run under a specific Python version, you should use that specific Python version in the shebang:

 #!/usr/bin/env python3.7

or even

 #!/usr/bin/python3.8

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