Python Forum
Problem Passing Arguement to do loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem Passing Arguement to do loop
#9
You're mixing Python 2.7 and 3.x code.
You could convert the scripts with 2to3 or other tools.
Another approach could be, to have two different installed Python-Versions.

Then you can't use sys.executable, if you run Python3.
In this case the sys.executable is Python3.

On the most Linux systems /usr/bin/python is a symlink to python2.7 and /usr/bin/python3 is a symlink to a 3.x version of the Python interpreter. On Mac it's similar I guess. On Windows you should use the programm py, which is installed together with newer python versions. I think it's delivered since Python 3.5.

This helper command finds the right interpreter for you. You just specify which version you want to have.


from subprocess import Popen, PIPE


def run_script(script_path, python_version=3, encoding='utf8'):
    return Popen(['py', f'-{python_version}', script_path], stdout=PIPE, stderr=PIPE, encoding=encoding)
To use Python 27, version have to be 27. If you want to have any available Python 2 version, you specify just 2 as version number.
The same with Python 3. If you want to have a Python 3 interpreter, just use 3 as version. Python 3.7.x is version 37. The dot is not used.

Another error is in the file "/sdc1/Apps/Modeling/FileEditing/untitled1m2.py", line 12
proc = subprocess.Popen(command, stdout=subprocess.PIPE, stdout=subprocess.PIPE)
2 times stdout. The second one should be stderr. Just a copy paste error.

Additionally I added encoding to subprocess. Then you get native strings instead of bytes.
With no specification of encoding, you get raw-bytes. This is interesting, if a program sends binary data to stdout.
You may have to change the encoding to another, if you use Python 2.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
RE: Problem Passing Arguement to do loop - by DeaD_EyE - May-11-2019, 11:58 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  problem program runs in a loop jasserin 0 110 May-18-2024, 03:07 PM
Last Post: jasserin
  While Loop Problem Benno2805 1 612 Sep-06-2023, 04:51 PM
Last Post: deanhystad
  Loop reading csv file problem faustineaiden 1 1,610 Dec-11-2021, 08:40 AM
Last Post: ibreeden
  Infinite loop problem Zirconyl 5 3,069 Nov-16-2020, 09:06 AM
Last Post: DeaD_EyE
  Dataframe mean calculation problem: do we have to loop? sparkt 1 2,213 Aug-28-2020, 02:41 PM
Last Post: sparkt
  Python loop problem Kristenl2784 11 5,209 Jun-18-2020, 07:22 PM
Last Post: buran
  Problem with append list in loop michaelko03 0 1,716 Feb-16-2020, 07:04 PM
Last Post: michaelko03
  problem with for loop using integers python_germ 5 3,089 Aug-31-2019, 11:42 AM
Last Post: jefsummers
  problem in loop roseojha 3 2,348 Aug-26-2019, 09:03 AM
Last Post: perfringo
  Nested while loop problem + turtle DreamingInsanity 3 3,026 Jul-06-2019, 02:01 PM
Last Post: DreamingInsanity

Forum Jump:

User Panel Messages

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