Python Forum

Full Version: switching to python3
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
i have found that a few of my scripts, while functional in python2, work better (or at least the way i prefer) in python3. for example: the attribute "st_mtime_ns" is available in the result from os.lstat(filename) only in python3 (not in python2). my code, in that case, checks for the existence of that attribute since it can also be absent for other reasons, and deals with it appropriately. so what i am doing is inserting the code shown below, into the script code file (not as a separate file) somewhere near the top after the imports (at least os and sys) are done.

the code being inserted:
    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)
maybe there is a better way to iterate over the PATH environment variable.

this probably only works in unix and linux. any other os experts around here? the best i know of to do is iterate over an empty list from an empty string where the PATH environment variable is not available.
for paths, I've been using pathlib, which created pure objects.
It still has a few quirks, but I expect they will be worked out by the next python release.
Here's some examples pay particular attention to the open statement:
>>> from pathlib import Path
>>> homepath = Path('.')
>>> print(homepath.resolve())
X:\python\m-p\m\MakerProject\venv\src
>>>
>>> datapath = homepath / 'data'
>>> print([file for file in datapath.iterdir()])
[WindowsPath('data/html'), WindowsPath('data/json'), WindowsPath('data/temp')]
>>> jsonpath = datapath / 'json'
>>> print([file for file in jsonpath.iterdir()])
[WindowsPath('data/json/images.json'), WindowsPath('data/json/images.txt'), WindowsPath('data/json/MakeText.bat'), WindowsPath('data/json/rfc_index.json'), WindowsPath('data/json/rfc_index.txt')]
>>> mktxt = jsonpath / 'MakeText.bat'
>>> with mktxt.open() as f:
...     for line in f:
...          print(f)
...
<_io.TextIOWrapper name='data\\json\\MakeText.bat' mode='r' encoding='cp1252'>
<_io.TextIOWrapper name='data\\json\\MakeText.bat' mode='r' encoding='cp1252'>
>>>
since i am switching to python3 from python2, this thing must work in python2.
pathlib will not. A lot of other stuff won't either.
If you don't want to use Python 3 improvements, why bother
switching?
but my code shown in post #1 works fine in python2. i write lots of version agnostic code.
Your code has already been written (thanks stackoverflow:). Look at this
>>> from distutils.spawn import find_executable
>>> find_executable('python3')
'/usr/bin/python3'
In python 3, there is also shutil.which()
but shutil.which() is not in python2. the whole point is to switch from python2.
(Feb-23-2018, 06:11 AM)Skaperen Wrote: [ -> ]but shutil.which() is not in python2. the whole point is to switch from python2.
You can copy and paste the code of shutil.which() from the python 3 source tree to your python 2 program. Most of it should work !
the code i wrote works in the context i intended it for and accomplishes the goal i wrote it for. is there a good reason to disregard the principle "if it ain't broke don't ..."?
(Feb-24-2018, 03:31 AM)Skaperen Wrote: [ -> ]and accomplishes the goal i wrote it for
This is true on your platform, but one can expect that the standard library's code is multiplatform and also that some corner cases have been taken into account, which you have not yet anticipated. It means that my suggestion addresses your original post.
Pages: 1 2