0

I am trying to run a script from python subprocess.

subprocess.call('set APPID=Testforscannedpdf',shell=True)
subprocess.call('set PWD=XXXXXXXXXXXX',shell=True)
subprocess.call('C:\\Users\\AppData\\Local\\Continuum\\anaconda3\\python.exe C:\\Users\\Desktop\\Parsing\\COF.pdf C:\\Users\\Desktop\\Parsing\\pdf_conv.py C:\\Users\\Desktop\\Parsing\\out\\outputs.txt',shell=True)

When I run it directly on Anaconda Prompt/command prompt I am able to run it. But when I am trying run it through subprocess I am unable to get the output. It always gives output as 1.

I know the error is in the 3rd 'subprocess.call' statement because of the spaces between arguments. But I unable to find a fix for it. I also tried passing the arguments in a list in 'subprocess.Popen' but no results again.

Please help. Thanks in advance!

India.Rocket
  • 1,225
  • 1
  • 8
  • 11
  • Hi! Looks like there are some duplicated questions: https://stackoverflow.com/questions/2502833/store-output-of-subprocess-popen-call-in-a-string ; https://stackoverflow.com/questions/2502833/store-output-of-subprocess-popen-call-in-a-string ; hope it hepls – Mikhail Stepanov Sep 03 '18 at 10:32
  • Not sure how it is related to my answer. I have already tried this 'import os os.chdir('C:/Users/Desktop/Parsing') subprocess.call('set APPID=Testforscannedpdf',shell=True) subprocess.call('set PWD=XXXXXXX',shell=True) subprocess.call(["python","pdf_conv.py","COF.pdf","outputs.txt"])' but no output – India.Rocket Sep 03 '18 at 10:39
  • Have you tried `Popen'?` `p = subprocess.Popen(["python","pdf_conv.py","COF.pdf","outputs.txt"], stdout=subprocess.PIPE)` then `out, err = p.communicate()`? – Mikhail Stepanov Sep 03 '18 at 10:42
  • It looks as if you're trying to call `python.exe` with three arguments which are respectively the paths to a PDF file, a Python script and a text file. Shouldn't the Python script be the first one? – nekomatic Sep 03 '18 at 10:44
  • This is what I am following 'https://ocrsdk.com/documentation/quick-start-guide/python-ocr-sdk/' and its working when I am writing the same statements directly in command prompt/Anaconda prompt. – India.Rocket Sep 03 '18 at 10:47
  • @MikhailStepanov Yup tried this but not working. This is what i am getting 'b'Uploading..\r\n'' That means it fails at uploading part i.e., COF.pdf – India.Rocket Sep 03 '18 at 10:50
  • The `subprocess` module, like its name reveals, creates a subprocess, changes its environment, and eventually quits. The parent Python process has no idea, and generally shouldn't care about or be affected by what the subprocess does to its own environment. – tripleee Sep 03 '18 at 11:46
  • Possible duplicate of [How do I make environment variable changes stick in Python?](https://stackoverflow.com/questions/488366/how-do-i-make-environment-variable-changes-stick-in-python) – tripleee Sep 03 '18 at 11:47
  • @MikhailStepanov err Out[72]: b"Fatal Python error: Py_Initialize: unable to load the file system codec\nModuleNotFoundError: No module named 'encodings'\n\nCurrent thread 0x00002ca4 (most recent call first):\n" – India.Rocket Sep 03 '18 at 11:53
  • I updated [my answer to this related question](https://stackoverflow.com/a/51950538/874188) with more details for this particular case. This is a common FAQ, and definitely a duplicate. – tripleee Sep 03 '18 at 12:01
  • @tripleee How should i apply in my case.Sorry, but I am unable to replicate it for my scenario with 2 environment variables i.e., APPID and PWD – India.Rocket Sep 03 '18 at 12:13
  • This is what I did :- import os os.chdir('C:/Users/Desktop/Parsing') subprocess.run(["python.exe","pdf_conv.py","COF.pdf","outputs.txt"],shell=True,env={'APPID':'Testforscannedpdf','PWD':'xxxxxxx' }) Output is: Out[86]: CompletedProcess(args=['python.exe', 'pdf_conv.py', 'COF.pdf', 'outputs.txt'], returncode=3221225781) – India.Rocket Sep 03 '18 at 12:18
  • You can't have `shell=True` if the first argument is not a string. I'm guessing there might be additional problems, which could be simply because you are using Windows. – tripleee Sep 03 '18 at 12:20

0 Answers0