I was wrong in my comment to original post. Suggested workaround does work. In case someone else has that issue here is what needs to be done in QGIS 2.0 before instantiating Manager().
# OSGeo4W does not bundle python in exec_prefix for python
path = os.path.abspath(os.path.join(sys.exec_prefix, '../../bin/pythonw.exe'))
mp.set_executable(path)
sys.argv = [ None ]
Note that this cannot be tested from python console as Windows lacks fork() and all multiprocessing statements shall be isolated.
If one wants to play around with multiprocessing and embedded python from OSGeo4W bundle outside of QGIS here is the code.
tst.py
import multiprocessing as mp
import sys, os
print("Non-isolated statement")
if __name__ == '__main__':
print("I'm in main module")
path = os.path.abspath(os.path.join(sys.exec_prefix, '../../bin/pythonw.exe'))
mp.set_executable(path)
print("Setting executable path to {:s}".format(path))
sys.argv = [ None ] # '../tst.py' __file__
mgr = mp.Manager()
print("I'm past Manager()")
tst.c
#include <Python.h>
#include <stdio.h>
int main(int argc, char * argv[]) {
char buf[10240] = {0};
size_t sz, res;
FILE *f;
Py_SetProgramName(argv[0]); /* optional but recommended */
Py_Initialize();
f = fopen("../tst.py", "r");
// obtain file size:
fseek(f, 0 , SEEK_END);
sz = ftell(f);
rewind(f);
res = fread(buf, 1, sz, f);
fclose(f);
PyRun_SimpleString(buf);
getchar();
/* PyRun_SimpleString("from time import time,ctime\n" */
/* "print 'Today is',ctime(time())\n"); */
Py_Finalize();
return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
find_package(PythonLibs)
include_directories(${PYTHON_INCLUDE_DIRS})
add_executable(tst tst.c)
target_link_libraries(tst ${PYTHON_LIBRARY})
if(MSVC)
set_target_properties(tst PROPERTIES LINK_FLAGS -NODEFAULTLIB:python27_d)
endif()
- From your OSGeo4W console, initialize your
vcvarsall.bat
building environment
- Create subfolder
build
(or alike) and cd to it
- Use
cmake-gui ..
to generate jom/nmake makefiles in build
folder provided all files are saved in the parent one
- Use nmake or jom to build tst.exe
- Try to run
tst
or python ..\tst.py
On Windows, your best option is currently to use the Python distribution that can be installed through OSGeo4W and install pip
through the OSGeo4W installer, too.
While pip
cannot install many packages that require linkage to external libraries (e.g. netCDF or GEOS) right from the package index, a workaround is to install these unofficial pre-compiled binaries from http://www.lfd.uci.edu/~gohlke/pythonlibs/ and install them in the OSGeo4W Shell through pip install path-to-downloaded-binary.whl
.
[Edit 2018] Now there is also the option to install QGIS via (Ana)conda:
conda install -c conda-forge qgis
Best Answer
If you're not 100% stuck with your Anaconda distro you can try this:
What I do is install QGIS from OSGEO4W rather than standalone. This way I get a complete python environment with QT and many more things.
After installing OSGEO4W you'll need pip. This is how to get it: https://trac.osgeo.org/osgeo4w/wiki/ExternalPythonPackages
Then you can pip install any python package you use from the Anaconda distro. I use NumPy, SciPy, CV2 etc.
Also you can install spyder from source after downloading the spyder source:
Here's an answer I posted for running spyder with QGIS (and GRASS) modules imported: Using Spyder IDE to develop python for Grass GIS, get 'ImportError: No module named grass.script'
I know I come from GIS so it was not a problem for me to have my python environment based on OSGEO4W, you might have other considerations naturally.