Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
switching to python3
#1
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.
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
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'>
>>>
Reply
#3
since i am switching to python3 from python2, this thing must work in python2.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
pathlib will not. A lot of other stuff won't either.
If you don't want to use Python 3 improvements, why bother
switching?
Reply
#5
but my code shown in post #1 works fine in python2. i write lots of version agnostic code.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
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()
Reply
#7
but shutil.which() is not in python2. the whole point is to switch from python2.
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
(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 !
Reply
#9
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 ..."?
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
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  switching from notebook to pycharm enterthevoid22 1 1,706 Oct-10-2020, 05:20 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

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