1

I have an application which I am trying to make platform-independent with Python.

I have Python 3.x installed in all 3 OS-(Mac,Win10,Ubuntu)

I have a python script batch.py that calls other python scripts from within itself like this:

import os
import argparse
import shutil

if __name__ == '__main__':
    parser.add_argument("-i", "--infolder", default="./pdfs", 
        help="Input folder with PDFs. Default: ./pdfs")
    args = parser.parse_args()
    infolder =args.infolder
    # Watermarking process    
    watermark_outfolder = tmp+'/pdfs_watermarked'
    if not os.path.exists(watermark_outfolder):
        os.makedirs(watermark_outfolder)
    else:
        for root, dirs, files in os.walk(watermark_outfolder):
            for f in files:
                os.unlink(os.path.join(root, f))
            for d in dirs:
                shutil.rmtree(os.path.join(root, d))
    watermark_command = 'python watermark.py --in '+infolder
    os.system(watermark_command) 

The problem I am having is in Linux, the scripts don't run when I am using os.system('python ... it works only when I run as os.system('python3 .... On the other hand when I am running it in Win10, it works with python but not with python3. I tried to put a check for the python version:

if sys.version_info[0] < 3:
        watermark_command = 'python watermark.py --in '+infolder
    else:
        watermark_command = 'python3 watermark.py --in '+infolder
    os.system(watermark_command)

Doing this doesn't help, it doesn't run in Win10

2
  • I think it's an alias problem, soneone actually using linux could confirm this. One easy fix is to setup the alias python to redirect to python3. On macOS I had to do so in the .zshrc file. I believe you have the same problem on macOS but you don't spot it as the command python3 is linked to python 3 and the command python is linked to python 2 (default install on macOS). Example in my .zshrc file: alias python=/usr/local/bin/python3 Commented Nov 5, 2020 at 8:29
  • Script doesn't run. Can you elaborate more with the error trace or something? Also, is the script watermark.py also platform dependent by any chance? Commented Nov 5, 2020 at 8:29

1 Answer 1

7

two things:

(1) don't use os.system unless you want to get shell injected (prefer subprocess.call for example)

imagine if your infolder was named '; touch pwnd' (or something more nefarious!)

(2) use sys.executable in place of python or python3. sys.executable refers to the executable you're running with

putting that all together, you'd have something like this:

subprocess.call((sys.executable, 'watermark.py', '--in', infolder))
Sign up to request clarification or add additional context in comments.

1 Comment

(2) assumes the first script is executed by python 3. This should be checked beforehand.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.