2

I'm using QGIS 3.4 and Python 3.7.0 on Windows 7.

From the Python console of QGIS, I created a test script ("test.py") which contains the following line:

print('Hello)

and this is saved in C:\OSGeo4W64\bin\test.py

I want to run this script from outside QGIS.

I tried opening "python-qgis.bat" which I found in C:\OSGeo4W64\bin, and typed:

test.py

but got an error saying [NameError: name 'test' is not defined].

The code itself works fine from QGIS, but because I want to run it from outside QGIS, do I need all the import... etc at the beginning of the script?

What do I need to do?

2
  • print is a builtin so you don't need imports but you will eventually as you start using QGIS objects, try using the full path to the file C:\OSGeo4W64\bin\test.py and see if that helps but I suspect the problem is the python console is looking for an object called 'test' with 'py' method, have a read of stackoverflow.com/questions/17247471/… about calling a script from a python console. Dec 12, 2018 at 3:14
  • 3
    You should get a syntax error, due to the missing quote.
    – Vince
    Dec 12, 2018 at 3:18

1 Answer 1

2

As commented by @MichaelStimson:

print is a builtin so you don't need imports but you will eventually as you start using QGIS objects, try using the full path to the file C:\OSGeo4W64\bin\test.py and see if that helps but I suspect the problem is the python console is looking for an object called 'test' with 'py' method, have a read of https://stackoverflow.com/questions/17247471/how-to-run-a-python-script-from-idle-interactive-shell about calling a script from a python console.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.