Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
in python 2 and python 3
#11
(Feb-13-2017, 02:45 AM)Skaperen Wrote: the script people download needs to work without them downloading or installing anything else, nor having done so in the past, whether they run it on a recent python 2 or run it on a recent python 3
You can freeze the code so user don't even need Python,it depend on what program is suppose to do.
I have done done with my Norwegian television program,which work on Windows and Linux.
I guess over 50% of my Windows user don't even know what Python is,they would guess a snake Think

The other can eg be pip install my_module and pip3 install my_module is all user do.
Then all method over here can be use,if you look at code from Requests.
You see check of Python version then a simple if is_py2: elif is_py3:

There is no requirement these day to support Python 2.x.
You see more and more module that has Python 3.5 or higher as requirement.
Reply
#12
(Feb-13-2017, 04:21 AM)metulburr Wrote:
Quote:the script people download needs to work without them downloading or installing anything else, nor having done so in the past, whether they run it on a recent python 2 or run it on a recent python 3.
the module six, and sys are in the standard library so they wont have to install anything extra either way.

then module six is the way to go.  does anyone know what the earliest version is that six was included in?

(Feb-13-2017, 11:37 AM)ichabod801 Wrote:
(Feb-13-2017, 04:21 AM)metulburr Wrote: he is saying to utilize python3.x where raw_input is no longer defined to determine python version

Well, not exactly. What I meant to say was that in some cases, you don't need sys to determine the version. In that case I had a script that I wanted to work in either 2.x or 3.x, but I wanted to use the 3.x version of input (which is 2.x's raw_input) no matter what. I wasn't suggesting that you use raw_input's existence as a way to test for version if you wanted to do something else across the 2/3 barrier.
if the program is using input() then this can be done.

but what if it isn't

i have not used input() since my first python script (and back then it was module pexpect, not really input()).

write a function that reads a file and counts the number lines in it (an example; use another example, if you prefer).  test the argument, if it is a, or any kind of, string, work with that as the file name (open it), otherwise test if it is, or assume it it, an open file object.  how would you (prefer to) do this (do not install anything, but you can use module six)?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#13
(Feb-17-2017, 05:10 AM)Skaperen Wrote: if the program is using input() then this can be done.

but what if it isn't

? Then you see if you can do a similar try/except, and if you can't you use sys.version.

(Feb-17-2017, 05:10 AM)Skaperen Wrote: write a function that reads a file and counts the number lines in it (an example; use another example, if you prefer). test the argument, if it is a, or any kind of, string, work with that as the file name (open it), otherwise test if it is, or assume it it, an open file object. how would you (prefer to) do this (do not install anything, but you can use module six)?

? This is a typing issue, not a 2/3 issue.

def read_it(read_file):
    if isinstance(read_file, str):
        read_file = open(read_file)
    for line in read_file:
        ...
If you are saying that the type you test against might change, then you can do the same thing as input. Create a STR_CLASSES tuple to test against. If there is a NameError because you are trying to access 3.x classes, use the 2.x classes.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#14
sys.version_info seems to be the simple generic way to go, expressed one way for all cases (making usage easy).
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Forum Jump:

User Panel Messages

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