0

This might be just a syntax error in Windows Powershell, but I'm not able to run a python script with a specific python version (I have two installations).

I have a batch file in which the following command works:

"C:\Program Files\QGIS 2.18\bin\python.exe" myscript.py

But if I open a PowerShell in the folder containing the python script, I get an error:

At line:1 char:45
+ ... C:\Program Files\QGIS 2.18\bin\python.exe" myscript.py
+                                                ~~~~~~~~~~~~~~~~~~~~~~~~~~
Unexpected token 'myscript.py' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnexpectedToken

I tried also without double-quotes, with single-quotes, and by specifying the path also to the python script:

"C:\Program Files\QGIS 2.18\bin\python.exe" C:\mydata\myscript.py

With quotes on both parts:

"C:\Program Files\QGIS 2.18\bin\python.exe" "C:\mydata\myscript.py"

But with a similar error.

What is the correct way to write this command?

7
  • The reason I believe this is happening inside the bin\python.exe when you run python.exe myscript.py is because myscript.py doesn't exist in bin/ of QGIS. Instead go to mydata/ directory and then run C:\Program Files\QGIS 2.18\bin\python.exe myscript.py which will use the python.exe path provided and then run myscript.py present in the mydata directory. Commented Nov 6, 2017 at 16:01
  • Thanks, unfortunately however, as specified in the question, I am already in C:\mydata which is where I launch the PowerShell. It is because that didn't work that I included the path to the .py file in the first place. Commented Nov 6, 2017 at 16:05
  • If you have python on your path, what do you get when you run where python on your cmd? Commented Nov 6, 2017 at 16:08
  • In cmd, that returns the paths for both of my installs: C:\ProgramData\Anaconda3\python.exe C:\Program Files\QGIS 2.18\bin\python.exe Commented Nov 6, 2017 at 16:13
  • What happens when you run "C:\Program Files\QGIS 2.18\bin\python.exe" myscript.py from cmd instead of ps? Commented Nov 6, 2017 at 16:31

1 Answer 1

2

Use & Call operator or . Dot sourcing operator as follows (choose one):

  • . "C:\Program Files\QGIS 2.18\bin\python.exe" "C:\mydata\myscript.py"
  • & "C:\Program Files\QGIS 2.18\bin\python.exe" "C:\mydata\myscript.py"
  • . "C:\Program Files\QGIS 2.18\bin\python.exe" "myscript.py"
  • & "C:\Program Files\QGIS 2.18\bin\python.exe" "myscript.py"

Explanation:

In Powershell, "a string" at start of a line means the same as Write-Output "a string" (see Example 1 in Write-Output cmdlet documentation). Hence, in the first example "C:\Program Files\QGIS 2.18\bin\python.exe" myscript.py

  • "C:\Program Files\QGIS 2.18\bin\python.exe" = the 1st token of type string
    • => the powershell interpreter expects that the whole line represents a positional argument of the Write-Output at position 0 i.e. an array of PS objects (PSObject[]), and
  • myscript.py = an unexpected token.
Sign up to request clarification or add additional context in comments.

3 Comments

Many thanks, this is technically the correct answer: the command now works. Unfortunately, I still cannot run the script due to ImportError: No module named site. This might be unrelated to the actual question, and more related to how the QGIS Python version is organized, but if you have any idea how to resolve this that'd be great!
@sc28 You could ask another question about site module or edit your current question. Share all import … and from XXX import … statements from currently called myscript.py. FYI, in can get from my standard python versions: <module 'site' from 'C:\Python\Python27\lib\site.pyc'> and <module 'site' from 'C:\\Python\\Python35\\lib\\site.py'>. (Please add full traceback for ImportError: No module named site).
@sc28 Please ask another question about site module using tags python, QGIS. Please avoid extending scope/topic of current question.

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.