Skip to main content
some clarification
Source Link
Ali
  • 4.1k
  • 2
  • 19
  • 30

I have been able to replicate your problem on a Windows 7 system using QGIS 2.14 LTR.

Similar to your batch file, the QGIS install includes a python-qgis batch file which you can use to run scripts with the environment pre-configured. As I've tested on 2.14 LTR my batch file is %OSGEO4W_ROOT%\bin\python-qgis-ltr.bat ( depending on your version your file may not have the ltr part: python-qgis.bat). You should find that calling:%OSGEO4W_ROOT%\bin\python-qgis-ltr getScreenshot.py from a command window will run successfully.

The following is a screenshot after opening a command window in the QGIS 2.14\bin directory, running python-gqis-ltr.bat, and then from qgis.core import *

enter image description here

In order to run without the batch file you could try manually setting the environment variables as you have previously, but check against the contents of python-gqis-ltr.bat or python-gqis.bat depending on your version. It's worth noting that when you open the OSGeo4W shell it clears your PATH environment variable - so you will have to reset that if you go this route.

Alternatively, you can skip batch files altogether by including a couple of lines at the top of your script:

import os, sys # Append QGIS Python library to python search path sys.path.append(r'C:\Program Files\QGIS 2.14\apps\qgis-ltr\python') # Append location of DLLs to current system PATH envrionment variable os.environ['PATH'] += r";C:\Program Files\QGIS 2.14\apps\qgis-ltr\bin;" # Examine new PATH environment variable print os.environ['PATH'] # Try the import from qgis.core import * print 'Done!' 

This ensures the QGIS Python library and DLLs can be found. This will however fail if the DLLs and Python are not using the same architecture i.e. 32-bit versus 64-bit.


Update

The error you mention in your comment is happening due to the location of Python within the QGIS installation directories. A typical Python install has the standard libraries in the Lib directory, itself stored in same directory as the executable e.g. C:\Python27\python.exe and C:\Python27\Lib. When importing modules this Lib folder is found in the search path.

However, as QGIS stores the Python executable in the QGIS\bin directory and the libraries in QGIS\apps\Python27, the standard libraries are not found by default -. QGIS sets all of it up through a series of batch files, which is why the script currently runs successfuly in the OSGeo4W shell.

You can use the PYTHONHOME environment variable to specify where these libraries are; this is what QGIS uses - and you have the line in your batchcreateQGISmap.bat file.

Since site is automatically imported when you load up Python you need to have this location known to the system beforehand. So, setting this manually should work but will change it for your system and therefore impact other python installations i.e. they will all point to the same PYTHONHOME directory.

If you don't want to change PYTHONHOME permanently, or resort to something convoluted, sticking with a batch file is probably the way to go.

I have been able to replicate your problem on a Windows 7 system using QGIS 2.14 LTR.

Similar to your batch file, the QGIS install includes a python-qgis batch file which you can use to run scripts with the environment pre-configured. As I've tested on 2.14 LTR my batch file is %OSGEO4W_ROOT%\bin\python-qgis-ltr.bat ( depending on your version your file may not have the ltr part: python-qgis.bat). You should find that calling:%OSGEO4W_ROOT%\bin\python-qgis-ltr getScreenshot.py from a command window will run successfully.

The following is a screenshot after opening a command window in the QGIS 2.14\bin directory, running python-gqis-ltr.bat, and then from qgis.core import *

enter image description here

In order to run without the batch file you could try manually setting the environment variables as you have previously, but check against the contents of python-gqis-ltr.bat or python-gqis.bat depending on your version. It's worth noting that when you open the OSGeo4W shell it clears your PATH environment variable - so you will have to reset that if you go this route.

Alternatively, you can skip batch files altogether by including a couple of lines at the top of your script:

import os, sys # Append QGIS Python library to python search path sys.path.append(r'C:\Program Files\QGIS 2.14\apps\qgis-ltr\python') # Append location of DLLs to current system PATH envrionment variable os.environ['PATH'] += r";C:\Program Files\QGIS 2.14\apps\qgis-ltr\bin;" # Examine new PATH environment variable print os.environ['PATH'] # Try the import from qgis.core import * print 'Done!' 

This ensures the QGIS Python library and DLLs can be found. This will however fail if the DLLs and Python are not using the same architecture i.e. 32-bit versus 64-bit.


Update

The error you mention in your comment is happening due to the location of Python within the QGIS installation directories. A typical Python install has the standard libraries in the Lib directory, itself stored in same directory as the executable e.g. C:\Python27\python.exe and C:\Python27\Lib. When importing modules this Lib folder is found in the search path.

However, as QGIS stores the Python executable in the QGIS\bin directory and the libraries in QGIS\apps\Python27, the standard libraries are not found by default - QGIS sets all of it up through a series of batch files.

You can use the PYTHONHOME environment variable to specify where these libraries are; this is what QGIS uses - and you have the line in your batch file.

Since site is automatically imported when you load up Python you need to have this location known to the system beforehand. So, setting this manually should work but will change it for your system and therefore impact other python installations i.e. they will all point to the same PYTHONHOME directory.

If you don't want to change PYTHONHOME permanently, or resort to something convoluted, sticking with a batch file is probably the way to go.

I have been able to replicate your problem on a Windows 7 system using QGIS 2.14 LTR.

Similar to your batch file, the QGIS install includes a python-qgis batch file which you can use to run scripts with the environment pre-configured. As I've tested on 2.14 LTR my batch file is %OSGEO4W_ROOT%\bin\python-qgis-ltr.bat ( depending on your version your file may not have the ltr part: python-qgis.bat). You should find that calling:%OSGEO4W_ROOT%\bin\python-qgis-ltr getScreenshot.py from a command window will run successfully.

The following is a screenshot after opening a command window in the QGIS 2.14\bin directory, running python-gqis-ltr.bat, and then from qgis.core import *

enter image description here

In order to run without the batch file you could try manually setting the environment variables as you have previously, but check against the contents of python-gqis-ltr.bat or python-gqis.bat depending on your version. It's worth noting that when you open the OSGeo4W shell it clears your PATH environment variable - so you will have to reset that if you go this route.

Alternatively, you can skip batch files altogether by including a couple of lines at the top of your script:

import os, sys # Append QGIS Python library to python search path sys.path.append(r'C:\Program Files\QGIS 2.14\apps\qgis-ltr\python') # Append location of DLLs to current system PATH envrionment variable os.environ['PATH'] += r";C:\Program Files\QGIS 2.14\apps\qgis-ltr\bin;" # Examine new PATH environment variable print os.environ['PATH'] # Try the import from qgis.core import * print 'Done!' 

This ensures the QGIS Python library and DLLs can be found. This will however fail if the DLLs and Python are not using the same architecture i.e. 32-bit versus 64-bit.


Update

The error you mention in your comment is happening due to the location of Python within the QGIS installation directories. A typical Python install has the standard libraries in the Lib directory, itself stored in same directory as the executable e.g. C:\Python27\python.exe and C:\Python27\Lib. When importing modules this Lib folder is found in the search path.

However, as QGIS stores the Python executable in the QGIS\bin directory and the libraries in QGIS\apps\Python27, the standard libraries are not found by default. QGIS sets all of it up through a series of batch files, which is why the script currently runs successfuly in the OSGeo4W shell.

You can use the PYTHONHOME environment variable to specify where these libraries are; this is what QGIS uses - and you have the line in your createQGISmap.bat file.

Since site is automatically imported when you load up Python you need to have this location known to the system beforehand. So, setting this manually should work but will change it for your system and therefore impact other python installations i.e. they will all point to the same PYTHONHOME directory.

If you don't want to change PYTHONHOME permanently, or resort to something convoluted, sticking with a batch file is probably the way to go.

added link to site module documentation
Source Link
Ali
  • 4.1k
  • 2
  • 19
  • 30

I have been able to replicate your problem on a Windows 7 system using QGIS 2.14 LTR.

Similar to your batch file, the QGIS install includes a python-qgis batch file which you can use to run scripts with the environment pre-configured. As I've tested on 2.14 LTR my batch file is %OSGEO4W_ROOT%\bin\python-qgis-ltr.bat ( depending on your version your file may not have the ltr part: python-qgis.bat). You should find that calling:%OSGEO4W_ROOT%\bin\python-qgis-ltr getScreenshot.py from a command window will run successfully.

The following is a screenshot after opening a command window in the QGIS 2.14\bin directory, running python-gqis-ltr.bat, and then from qgis.core import *

enter image description here

In order to run without the batch file you could try manually setting the environment variables as you have previously, but check against the contents of python-gqis-ltr.bat or python-gqis.bat depending on your version. It's worth noting that when you open the OSGeo4W shell it clears your PATH environment variable - so you will have to reset that if you go this route.

Alternatively, you can skip batch files altogether by including a couple of lines at the top of your script:

import os, sys # Append QGIS Python library to python search path sys.path.append(r'C:\Program Files\QGIS 2.14\apps\qgis-ltr\python') # Append location of DLLs to current system PATH envrionment variable os.environ['PATH'] += r";C:\Program Files\QGIS 2.14\apps\qgis-ltr\bin;" # Examine new PATH environment variable print os.environ['PATH'] # Try the import from qgis.core import * print 'Done!' 

This ensures the QGIS Python library and DLLs can be found. This will however fail if the DLLs and Python are not using the same architecture i.e. 32-bit versus 64-bit.


Update

The error you mention in your comment is happening due to the location of Python within the QGIS installation directories. A typical Python install has the standard libraries in the Lib directory, itself stored in same directory as the executable e.g. C:\Python27\python.exe and C:\Python27\Lib. When importing modules this Lib folder is found in the search path.

However, as QGIS stores the Python executable in the QGIS\bin directory and the libraries in QGIS\apps\Python27, the standard libraries are not found by default - QGIS sets all of it up through a series of batch files.

You can use the PYTHONHOME environment variable to specify where these libraries are; this is what QGIS uses - and you have the line in your batch file.

Since site is automatically importedautomatically imported when you load up Python you need to have this location known to the system beforehand. So, setting this manually should work but will change it for your system and therefore impact other python installations i.e. they will all point to the same PYTHONHOME directory.

If you don't want to change PYTHONHOME permanently, or resort to something convoluted, sticking with a batch file is probably the way to go.

I have been able to replicate your problem on a Windows 7 system using QGIS 2.14 LTR.

Similar to your batch file, the QGIS install includes a python-qgis batch file which you can use to run scripts with the environment pre-configured. As I've tested on 2.14 LTR my batch file is %OSGEO4W_ROOT%\bin\python-qgis-ltr.bat ( depending on your version your file may not have the ltr part: python-qgis.bat). You should find that calling:%OSGEO4W_ROOT%\bin\python-qgis-ltr getScreenshot.py from a command window will run successfully.

The following is a screenshot after opening a command window in the QGIS 2.14\bin directory, running python-gqis-ltr.bat, and then from qgis.core import *

enter image description here

In order to run without the batch file you could try manually setting the environment variables as you have previously, but check against the contents of python-gqis-ltr.bat or python-gqis.bat depending on your version. It's worth noting that when you open the OSGeo4W shell it clears your PATH environment variable - so you will have to reset that if you go this route.

Alternatively, you can skip batch files altogether by including a couple of lines at the top of your script:

import os, sys # Append QGIS Python library to python search path sys.path.append(r'C:\Program Files\QGIS 2.14\apps\qgis-ltr\python') # Append location of DLLs to current system PATH envrionment variable os.environ['PATH'] += r";C:\Program Files\QGIS 2.14\apps\qgis-ltr\bin;" # Examine new PATH environment variable print os.environ['PATH'] # Try the import from qgis.core import * print 'Done!' 

This ensures the QGIS Python library and DLLs can be found. This will however fail if the DLLs and Python are not using the same architecture i.e. 32-bit versus 64-bit.


Update

The error you mention in your comment is happening due to the location of Python within the QGIS installation directories. A typical Python install has the standard libraries in the Lib directory, itself stored in same directory as the executable e.g. C:\Python27\python.exe and C:\Python27\Lib. When importing modules this Lib folder is found in the search path.

However, as QGIS stores the Python executable in the QGIS\bin directory and the libraries in QGIS\apps\Python27, the standard libraries are not found by default - QGIS sets all of it up through a series of batch files.

You can use the PYTHONHOME environment variable to specify where these libraries are; this is what QGIS uses - and you have the line in your batch file.

Since site is automatically imported when you load up Python you need to have this location known to the system beforehand. So, setting this manually should work but will change it for your system and therefore impact other python installations i.e. they will all point to the same PYTHONHOME directory.

If you don't want to change PYTHONHOME permanently, or resort to something convoluted, sticking with a batch file is probably the way to go.

I have been able to replicate your problem on a Windows 7 system using QGIS 2.14 LTR.

Similar to your batch file, the QGIS install includes a python-qgis batch file which you can use to run scripts with the environment pre-configured. As I've tested on 2.14 LTR my batch file is %OSGEO4W_ROOT%\bin\python-qgis-ltr.bat ( depending on your version your file may not have the ltr part: python-qgis.bat). You should find that calling:%OSGEO4W_ROOT%\bin\python-qgis-ltr getScreenshot.py from a command window will run successfully.

The following is a screenshot after opening a command window in the QGIS 2.14\bin directory, running python-gqis-ltr.bat, and then from qgis.core import *

enter image description here

In order to run without the batch file you could try manually setting the environment variables as you have previously, but check against the contents of python-gqis-ltr.bat or python-gqis.bat depending on your version. It's worth noting that when you open the OSGeo4W shell it clears your PATH environment variable - so you will have to reset that if you go this route.

Alternatively, you can skip batch files altogether by including a couple of lines at the top of your script:

import os, sys # Append QGIS Python library to python search path sys.path.append(r'C:\Program Files\QGIS 2.14\apps\qgis-ltr\python') # Append location of DLLs to current system PATH envrionment variable os.environ['PATH'] += r";C:\Program Files\QGIS 2.14\apps\qgis-ltr\bin;" # Examine new PATH environment variable print os.environ['PATH'] # Try the import from qgis.core import * print 'Done!' 

This ensures the QGIS Python library and DLLs can be found. This will however fail if the DLLs and Python are not using the same architecture i.e. 32-bit versus 64-bit.


Update

The error you mention in your comment is happening due to the location of Python within the QGIS installation directories. A typical Python install has the standard libraries in the Lib directory, itself stored in same directory as the executable e.g. C:\Python27\python.exe and C:\Python27\Lib. When importing modules this Lib folder is found in the search path.

However, as QGIS stores the Python executable in the QGIS\bin directory and the libraries in QGIS\apps\Python27, the standard libraries are not found by default - QGIS sets all of it up through a series of batch files.

You can use the PYTHONHOME environment variable to specify where these libraries are; this is what QGIS uses - and you have the line in your batch file.

Since site is automatically imported when you load up Python you need to have this location known to the system beforehand. So, setting this manually should work but will change it for your system and therefore impact other python installations i.e. they will all point to the same PYTHONHOME directory.

If you don't want to change PYTHONHOME permanently, or resort to something convoluted, sticking with a batch file is probably the way to go.

added 32 characters in body
Source Link
Ali
  • 4.1k
  • 2
  • 19
  • 30

I have been able to replicate your problem on a Windows 7 system using QGIS 2.14 LTR.

Similar to your batch file, the QGIS install includes a python-qgis batch file which you can use to run scripts with the environment pre-configured. As I've tested on 2.14 LTR my batch file is %OSGEO4W_ROOT%\bin\python-qgis-ltr.bat ( depending on your version your file may not have the ltr part: python-qgis.bat). You should find that calling:%OSGEO4W_ROOT%\bin\python-qgis-ltr getScreenshot.py from a command window will run successfully.

The following is a screenshot after opening a command window in the QGIS 2.14\bin directory, running python-gqis-ltr.bat, and then from qgis.core import *

enter image description here

In order to run without the batch file you could try manually setting the environment variables as you have previously, but check against the contents of python-gqis-ltr.bat or python-gqis.bat depending on your version. It's worth noting that when you open the OSGeo4W shell it clears your PATH environment variable - so you will have to reset that if you go this route.

Alternatively, you can skip batch files altogether by including a couple of lines at the top of your script:

import os, sys # Append QGIS Python library to python search path sys.path.append(r'C:\Program Files\QGIS 2.14\apps\qgis-ltr\python') # Append location of DLLs to current system PATH envrionment variable os.environ['PATH'] += r";C:\Program Files\QGIS 2.14\apps\qgis-ltr\bin;" # Examine new PATH environment variable print os.environ['PATH'] # Try the import from qgis.core import * print 'Done!' 

This ensures the QGIS Python library and DLLs can be found. This will however fail if the DLLs and Python are not using the same architecture i.e. 32-bit versus 64-bit.


Update

The error you mention in your comment is happening due to the location of Python within the QGIS installation directories. A typical Python install has the standard libraries in the Lib directory, itself stored in same directory as the executable e.g. C:\Python27\python.exe and C:\Python27\Lib. When importing modules this Lib folder is found in the search path.

However, as QGIS stores the Python executable in the QGIS\bin directory and the libraries in QGIS\apps\Python27, the standard libraries are not found by default - QGIS sets all of it up through a series of batch files.

You can use the PYTHONHOME environment variable to specify where these libraries are; this is what QGIS uses - and you have the line in your batch file.

Since site is automatically imported when you load up Python you need to have this location known to the system beforehand. So, setting this manually should work but will change it for your system and therefore impact other python installations i.e. they will all point to the same PYTHONHOME directory.

If you dontdon't want to change PYTHONHOME permanently then, or resort to something convoluted, sticking with a batch file is probably the way to go.

I have been able to replicate your problem on a Windows 7 system using QGIS 2.14 LTR.

Similar to your batch file, the QGIS install includes a python-qgis batch file which you can use to run scripts with the environment pre-configured. As I've tested on 2.14 LTR my batch file is %OSGEO4W_ROOT%\bin\python-qgis-ltr.bat ( depending on your version your file may not have the ltr part: python-qgis.bat). You should find that calling:%OSGEO4W_ROOT%\bin\python-qgis-ltr getScreenshot.py from a command window will run successfully.

The following is a screenshot after opening a command window in the QGIS 2.14\bin directory, running python-gqis-ltr.bat, and then from qgis.core import *

enter image description here

In order to run without the batch file you could try manually setting the environment variables as you have previously, but check against the contents of python-gqis-ltr.bat or python-gqis.bat depending on your version. It's worth noting that when you open the OSGeo4W shell it clears your PATH environment variable - so you will have to reset that if you go this route.

Alternatively, you can skip batch files altogether by including a couple of lines at the top of your script:

import os, sys # Append QGIS Python library to python search path sys.path.append(r'C:\Program Files\QGIS 2.14\apps\qgis-ltr\python') # Append location of DLLs to current system PATH envrionment variable os.environ['PATH'] += r";C:\Program Files\QGIS 2.14\apps\qgis-ltr\bin;" # Examine new PATH environment variable print os.environ['PATH'] # Try the import from qgis.core import * print 'Done!' 

This ensures the QGIS Python library and DLLs can be found. This will however fail if the DLLs and Python are not using the same architecture i.e. 32-bit versus 64-bit.


Update

The error you mention in your comment is happening due to the location of Python within the QGIS installation directories. A typical Python install has the standard libraries in the Lib directory, itself stored in same directory as the executable e.g. C:\Python27\python.exe and C:\Python27\Lib. When importing modules this Lib folder is found in the search path.

However, as QGIS stores the Python executable in the QGIS\bin directory and the libraries in QGIS\apps\Python27, the standard libraries are not found by default - QGIS sets all of it up through a series of batch files.

You can use the PYTHONHOME environment variable to specify where these libraries are; this is what QGIS uses - and you have the line in your batch file.

Since site is automatically imported when you load up Python you need to have this location known to the system beforehand. So, setting this manually should work but will change it for your system and therefore impact other python installations i.e. they will all point to the same PYTHONHOME directory.

If you dont want to change PYTHONHOME permanently then sticking with a batch file is probably the way to go.

I have been able to replicate your problem on a Windows 7 system using QGIS 2.14 LTR.

Similar to your batch file, the QGIS install includes a python-qgis batch file which you can use to run scripts with the environment pre-configured. As I've tested on 2.14 LTR my batch file is %OSGEO4W_ROOT%\bin\python-qgis-ltr.bat ( depending on your version your file may not have the ltr part: python-qgis.bat). You should find that calling:%OSGEO4W_ROOT%\bin\python-qgis-ltr getScreenshot.py from a command window will run successfully.

The following is a screenshot after opening a command window in the QGIS 2.14\bin directory, running python-gqis-ltr.bat, and then from qgis.core import *

enter image description here

In order to run without the batch file you could try manually setting the environment variables as you have previously, but check against the contents of python-gqis-ltr.bat or python-gqis.bat depending on your version. It's worth noting that when you open the OSGeo4W shell it clears your PATH environment variable - so you will have to reset that if you go this route.

Alternatively, you can skip batch files altogether by including a couple of lines at the top of your script:

import os, sys # Append QGIS Python library to python search path sys.path.append(r'C:\Program Files\QGIS 2.14\apps\qgis-ltr\python') # Append location of DLLs to current system PATH envrionment variable os.environ['PATH'] += r";C:\Program Files\QGIS 2.14\apps\qgis-ltr\bin;" # Examine new PATH environment variable print os.environ['PATH'] # Try the import from qgis.core import * print 'Done!' 

This ensures the QGIS Python library and DLLs can be found. This will however fail if the DLLs and Python are not using the same architecture i.e. 32-bit versus 64-bit.


Update

The error you mention in your comment is happening due to the location of Python within the QGIS installation directories. A typical Python install has the standard libraries in the Lib directory, itself stored in same directory as the executable e.g. C:\Python27\python.exe and C:\Python27\Lib. When importing modules this Lib folder is found in the search path.

However, as QGIS stores the Python executable in the QGIS\bin directory and the libraries in QGIS\apps\Python27, the standard libraries are not found by default - QGIS sets all of it up through a series of batch files.

You can use the PYTHONHOME environment variable to specify where these libraries are; this is what QGIS uses - and you have the line in your batch file.

Since site is automatically imported when you load up Python you need to have this location known to the system beforehand. So, setting this manually should work but will change it for your system and therefore impact other python installations i.e. they will all point to the same PYTHONHOME directory.

If you don't want to change PYTHONHOME permanently, or resort to something convoluted, sticking with a batch file is probably the way to go.

added 22 characters in body
Source Link
Ali
  • 4.1k
  • 2
  • 19
  • 30
Loading
update regarding the site module
Source Link
Ali
  • 4.1k
  • 2
  • 19
  • 30
Loading
expanded on update with formatting
Source Link
Ali
  • 4.1k
  • 2
  • 19
  • 30
Loading
added 1150 characters in body
Source Link
Ali
  • 4.1k
  • 2
  • 19
  • 30
Loading
architecture
Source Link
Ali
  • 4.1k
  • 2
  • 19
  • 30
Loading
elaborated
Source Link
Ali
  • 4.1k
  • 2
  • 19
  • 30
Loading
elaborated
Source Link
Ali
  • 4.1k
  • 2
  • 19
  • 30
Loading
added 791 characters in body
Source Link
Ali
  • 4.1k
  • 2
  • 19
  • 30
Loading
added 176 characters in body
Source Link
Ali
  • 4.1k
  • 2
  • 19
  • 30
Loading
added 11 characters in body
Source Link
Ali
  • 4.1k
  • 2
  • 19
  • 30
Loading
Source Link
Ali
  • 4.1k
  • 2
  • 19
  • 30
Loading