1

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?

  • 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. – Michael Stimson Dec 12 '18 at 3:14
  • 3
    You should get a syntax error, due to the missing quote. – Vince Dec 12 '18 at 3:18
0

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, privacy policy and cookie policy

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