Python Forum
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
code to run in python3
#1
how do you guys feel about a python script (command or GUI app, not library code) that has code which checks the version of python it is running under, and if running under a version less than what it needs, tries to re-run itself under the version it needs, if available, and if unable, prints a suitable message for a naive user and aborts.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
I like the idea, but this would be enough for me:

maybe:
   use 3.6.x
instead of
    if sys.version_info < (3,6): print("ERROR: This script needs Python 3.6 or later"); exit(99)
Reply
#3
It would be too dependent on the system software (python version) being there to use.
If it were installed in an obscure directory, and not in the path, it would lead to issues.
I don't (think) like the idea, would have to be convinced.
What about security issues?
Reply
#4
my code scans the directories in the 'PATH' environment variable.
if sys.version_info.major<3:
    for p in os.environ.get('PATH','').split(':'):
        if p and os.path.exists(p+'/python3'):
            os.execvp(p+'/python3',['python3']+sys.argv)
or i could just let os.execvp do the PATH scan.
Output:
lt1/forums /home/forums 14> py     Python 2.7.12 (default, Nov 19 2016, 06:48:10) [GCC 5.4.0 20160609] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import os >>> os.execvp('python3',['python3']) Python 3.5.2 (default, Sep 14 2017, 22:51:06) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import os >>> try: ...     os.execvp('python4',['python4']) ... except FileNotFoundError: ...     print("oops! you don't have python4") ... oops! you don't have python4 >>> os.execvp('python',['python']) Python 2.7.12 (default, Nov 19 2016, 06:48:10) [GCC 5.4.0 20160609] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import os >>> try: ...     os.execvp('python4',['python4']) ... except OSError: ...     print("oops! you don't have python4") ... oops! you don't have python4 >>> lt1/forums /home/forums 16>
yes, i want to know about any security issues you see.  i don't really see any.  what could the user do?  change the code?  change the interpreter?  run something else?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
Can I do this shorter or better ?

 if sys.version_info < (3,6): print("ERROR: This script needs Python 3.6 or later"); exit(99) 
Reply
#6
Any message or documentation of what platform is needed Python 2 or 3,Windows,Linux,Mac?
Is a lot better that no info,which confuse people especially if new to Python.

There are build in stuff when make module/package,so can block it from install to wrong version.
If i copy out setup.py from my tutorial here.
# setup.py
from setuptools import setup
 
__author__ = 'snippsat'
 
setup(
   name="web_title",
   version='0.1',
   py_modules=['find_title'],
   description="Find web site title",
   url='https://python-forum.io/index.php',
   author_email='[email protected]',
   python_requires='>=3.4',
   classifiers=[
       'Programming Language :: Python :: 3.4',
       'Programming Language :: Python :: 3.5',
       'Programming Language :: Python :: 3.6',
    ],
   # These will be installed when pip wheel run
   install_requires=[
       'beautifulsoup4',
       'requests',
       'lxml',
   ],
)
So python_requires='>=3.4',will only install wheel to 3.4 or newer.
classifiers in more meta search data and info about platform,
do not set any restriction List trove classifiers - PyPI

So if try to install wheel made with this setup.py to Python 2 this happens.
# Try 2.7
C:\web\dist
λ py -2.7 -m pip install web_title-0.1-py3-none-any.whl
web_title-0.1-py3-none-any.whl is not a supported wheel on this platform.

# 3.6 and it work
C:\web\dist
λ pip install web_title-0.1-py3-none-any.whl
Processing c:\web\dist\web_title-0.1-py3-none-any.whl
Requirement already satisfied: beautifulsoup4 in c:\python36\lib\site-packages (from web-title==0.1)
Requirement already satisfied: requests in c:\python36\lib\site-packages (from web-title==0.1)
Requirement already satisfied: lxml in c:\python36\lib\site-packages (from web-title==0.1)
c:\python36\lib\site-packages (from requests->web-title==0.1)
Requirement already satisfied: urllib3<1.23,>=1.21.1 in c:\python36\lib\site-packages (from requests->web-title==0.1)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in c:\python36\lib\site-packages (from requests->web-title==0.1)
Installing collected packages: web-title
Successfully installed web-title-0.1

Quote:that has code which checks the version of python it is running under, and if running under a version less than what it needs, tries to re-run itself under the version it needs, 
pipenv has in fact now build stuff for this Automatic Python Installation.
Will not re run it self but ask if want to install version needed(work on Linux and Mac but also need that pyenv is configured).
Reply
#7
(Nov-12-2017, 01:44 PM)heiner55 Wrote: Can I do this shorter or better ?

 if sys.version_info < (3,6): print("ERROR: This script needs Python 3.6 or later"); exit(99) 

my emphasis is on making it switch to the correct version.  do you think that is a bad idea?  or maybe it should anounce the switch?  or do you think it should just abort?

that tutorial was long and i could not read it all.  it started to get confusing as it seemed to be about packaging and installing.  and i could not figure out what the "wheel" thing was about.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#8
Sorry, my last post was a little bit off-topic.
If you need such a feature, then do it.
Reply
#9
but what about distributing such code?  i may sometimes need it, but if i put it in the code, it will always be in there.  i'm trying to teach interactive bash how to run .py code in the correct version of python when it runs the code not in the context of an executable but has to specify which interpreter.  in the command_not_found_handle() i have it check for existence of $name.py and if it exists and is readable, read one line to see if it has a #! line.  if it has one that has "py" in it then it is a python command.  if it also has a '2' and not a '3' then use python2.  if it has a '3' and not a '2' then use python3.  execute it (passing the args) under the selected python.  if it has neither '2' nor '3' or has both (yeah, odd) an environment variable PYTHON_DEFAULT_VERSION is not set, it tries guess.  it can pick the wrong version.  but there are also cases where the version does not matter (most of my code).  where it does not matter, i want a way to be specific about that (like maybe "#!/usr/bin/env pythonx") so it can always run under whatever is there.  but there is no standard for this.

fyi, the thing mentioned above lets me create "foo.py" and do "foo bar" to run it passing "bar" as an argument, if i included the #! in it.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#10
Hm! I don't know if it is a standard but when I run python2 I get python 2.x interpreter opened and waiting for me. Same with python3. I think these are shortcuts to whatever version is installed respectively for Python 2.x or Python 3.x. So in the shebang line python2 should work as well as python3. While python is shortcut to the default version for the system
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  What the difference between python2 and python3 when they running my code. lpangfeic 1 889 Nov-19-2019, 04:44 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020