DLL load failed with PyQGIS - python-2.7

I've already asked this question in the "gis.stackexchange" forum, but it seems to be more appropriate for this one.
I am having problems with Python Shell importing the module "qgis.core".
When I type "import qgis.core" the Shell ("idle.pyw") gives me this error:
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import qgis.core
ImportError: DLL load failed: Impossibile trovare il modulo specificato (Trad "Impossible to find the specified module").
I have already set the environment to point the right folders following "PyQGIS cookbook" instructions.
In my case, the paths are:
PYTHOPATH=C:\"QGIS_path"\apps\qgis\python;
Path=C:\"QGIS_path"\apps\qgis\bin.
Am I missing something? What are supposed to be the right libraries the Shell is unable to find?
I am working with windows 7 64 bit, QGIS Lisboa (version 1.8), Python 2.7.2 (the one that is included in the QGIS package).

I do not like to mix my python installations, so I add the paths on the fly.
The paths you need are below:
import sys
sys.path.extend([r"C:\Program Files\QGIS Valmiera\apps",r"C:\Program Files\QGIS Valmiera\apps\qgis\bin",r"C:\Program Files\QGIS Valmiera\apps\Python27"])
import qgis.core

Check this link import-qgis-core-in-pycharm-no-module-named-pyqt5-qtcore
also ignore the pycharm setup if you want.
It works for me,
setup-qgispy.bat
#ECHO off
set OSGEO4W_ROOT=C:\QGIS
call "%OSGEO4W_ROOT%\bin\o4w_env.bat"
call "%OSGEO4W_ROOT%\bin\qt5_env.bat"
call "%OSGEO4W_ROOT%\bin\py3_env.bat"
path %OSGEO4W_ROOT%\apps\qgis\bin;%PATH%
set QGIS_PREFIX_PATH=%OSGEO4W_ROOT%\apps\qgis
set GDAL_FILENAME_IS_UTF8=YES
set VSI_CACHE=TRUE
set VSI_CACHE_SIZE=1000000
set QT_PLUGIN_PATH=%OSGEO4W_ROOT%\apps\qgis\qtplugins;%OSGEO4W_ROOT%\apps\qt5\plugins
set PYTHONPATH=%OSGEO4W_ROOT%\apps\qgis\python
set PYTHONHOME=%OSGEO4W_ROOT%\apps\Python37
set PYTHONPATH=%OSGEO4W_ROOT%\apps\Python37\lib\site-packages;%PYTHONPATH%
set QT_QPA_PLATFORM_PLUGIN_PATH=%OSGEO4W_ROOT%\apps\Qt5\plugins\platforms
set QGIS_PREFIX_PATH=%OSGEO4W_ROOT%\apps\qgis
and then,
C:\> setup-qgispy.bat
C:\> python file.py

I think instead of sys.path.extend(). Copy the sys.path from qgis python console and use it. Hope this will work.

Related

Getting no module named _internals error while using Bloomberg API

I am currently using Python 2.7 and my OS is Windows 7. While attempting to use the Bloomberg API I am getting this error:
Traceback (most recent call last):
File "datagrab.py", line 1, in <module>
import blpapi, time, json
File "C:\Python27\lib\blpapi\__init__.py", line 5, in <module>
from .internals import CorrelationId
File "C:\Python27\lib\blpapi\internals.py", line 50, in <module>
_internals = swig_import_helper()
File "C:\Python27\lib\blpapi\internals.py", line 42, in swig_import_helper
import _internals
ImportError: No module named _internals
I have set my path variable to point to blpapi3_64.dll and also updated my bloomberg terminal. I have also moved the local blpapi API to a different directory but still the problem exists.
I am kind of new to this API in general. So can someone please guide me?
Thank you in advance!
From your question is sounds like maybe you have tried this, but just outlining one possible solution from the README in the Python Supported Release release available here.
Note that many Python installations add the current directory to the
module search path. If the Python interpreter is invoked from the
installer directory, such a configuration will attempt to use the
(incomplete) local blpapi directory as a module. If the above
import line fails with the message Import Error: No module named
_internals, move to a different directory before invoking python.
I know this question is a bit stale, but in case people end up here like me. Do you have the C++ version of blpapi? it is a requirement for the python api as mentioned here: https://www.bloomberg.com/professional/support/api-library/
so download the C++ zip installer, extract somewhere, and then add it as an environment variable so that the python api can find it:
Environment variable name: BLPAPI_ROOT
Value: C:\blp\blpapi_cpp_3.8.18.1 (THIS IS WHERE MINE IS INSTALLED, YOUR VALUE HERE MAY BE DIFFERENT)
Hope that helps!

Python 2.6 'import site' failed error with 2.7 installed

I have Python 2.7 operating correctly on Windows. I installed 2.6 in another folder. However, when I run 2.6 in IDLE or PowerShell, it does not recognize basic commands and cannot import installed libraries. Whenever I launch 2.6, the first thing that comes up is:
'import site' failed; use -v for traceback
I can't find anything on this error aside from some mentions with specific libraries. Trying again using -v suggests 2.6 is pulling libraries from 2.7 which is causing some of the errors.
It then launches, but doesn't recognize installed libraries. For example:
import numpy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named numpy
Similarly, simple commands like quit() or help result in:
Traceback (most recent call last): File "", line 1, in
NameError: name 'help' is not defined
I have tried reinstalling 2.6 and double-checked the registries are pointing to the right places. Virtualenv can't seem to run it either, so I'd like a solution that doesn't involve it...for now.
EDIT:
Running 2.6 with -v starts off like this:
PS C:\python26> python26 -v
installing zipimport hook
import zipimport # builtin
installed zipimport hook
C:\Python27\Lib\site.pyc matches >C:\Python27\Lib\site.py
import site # precompiled from >C:\Python27\Lib\site.pyc
For some reason, zipimport is pulling from 2.7, not 2.6.
I found a text_zipimport.py file, but it's beyond me.
Took a while, but I did solve this. When I installed 2.6, it did not add anything to the environment path. I'm working in windows, so here's what I did:
Start -> search for "environment"
Select "Edit environment variables for your account"
Click the variable Path and click Edit
Add "C:\Python26;C:\Python26\Scripts;" to the path
Ok
After that it loads properly.

python 2.7 windows ImportError: No module named OpenSSL

I try to make https server on windows using python 2.7, and this is my source. when i run it i got this error.
Traceback (most recent call last):
File "httpserver.py", line 38, in <module>
from OpenSSL import SSL
ImportError: No module named OpenSSL
Do i need to install that module first or is it default installed on my system?
Or maybe is there something wrong?
I think you have to install the OpenSSL modue.
Python uses below search path in sequence to find the module you are trying to import:
The home directory of the program
Diretories in environment variable PYTHONPATH
Standard library diretories
The contents of any .pth files
I guess the python program won't find anything in item 1 and 2 above.
So, try to check the standard library directories by using below:
import sys
print sys.path
It will return the paths to the standard library directories. Just check in those directories and if you don't see OpenSSL, then yes, you have to install the module first.

Python Tkinter throwing Tcl error

I am learning basic GUI in Python, and I came across a sample example to read file name from file explorer on Stack Overflow.
from Tkinter import Tk
from tkFileDialog import askopenfilename
Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing
filename = askopenfilename() # show an "Open" dialog box and return the path to the selected file
print(filename)
This particular script is working fine when I am trying to run it in IDLE, but the same is not running if I am trying from command prompt in windows 7.
Python Version: 2.7. Here is the output error which I get.
>>> from Tkinter import Tk
>>> from tkFileDialog import askopenfilename
>>> Tk().withdraw()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\Lib\lib-tk\Tkinter.py", line 1685, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: Can't find a usable init.tcl in the following directories:
C:/Python27/lib/tcl8.5 D:/PyProj/lib/tcl8.5 D:/lib/tcl8.5 D:/PyProj/library D:/library D:/tcl8.5.2/library D:/tcl8.5.2/library
This probably means that Tcl wasn't installed properly
Any pointer to what I am missing here can be of great help.
You just need to copy two folders from tcl folder to the Lib folder
tcl8.5 and tk8.5
In case you are using Virtualenv on Windows I found a solution here: https://github.com/pypa/virtualenv/issues/93
I copied the "tcl" folder from C:\Python27\ over to the root of the new Virtualenv, Tkinter.Tk() shows a new window without throwing an exception.
I am running Python 2.7 on Windows 7.
Hit a similar problem after installing Activestate Python and TCL. I found the following page solved the problem for me: ActiveState Python install problem. The fix was to copy the contents of C:\Python27\tcl into C:\Python27\Lib.
Another potential solution (given by user i-shenl in a different ActiveState thread on the same issue) is to set the environment variable $TCL_LIBRARY to point to the tcl library folder ("C:/Python27/tcl", in the question). If you set this system-wide or account-wide (via System Properties), it will affect other programs that use a TCL Library (if any are installed). If you're using Powershell, you can set this variable in your profile to limit its affects to programs run from the shell.
I hit the same problem on Ubuntu 17.04 with virtualenvwrapper for 64 bit Python 2.7
I add tk and tcl library paths in local postactivate script
Go to your virtualenv: workon your-env-name
Edit local postactiave script with your favourite editor, for ex:
gedit $VIRTUAL_ENV/bin/postactivate
Locate tk and tcl library paths. In postactivate script, export TK_LIBRARY and TCL_LIBRARY with appropriate paths. Add this lines to your script with modified paths:
TK_LIBRARY=/home/kamil/anaconda2/pkgs/tk-8.5
TKPATH=/home/kamil/anaconda2/pkgs/tk-8.5
TCL_LIBRARY=/home/kamil/anaconda2/lib/tcl8.5
export TCL_LIBRARY TK_LIBRARY TKPATH
Restart your virtualenv: deactivate and workon your-env-name again.
If you are hitting this kind of error in a python -m venv NAME kind of virtual environment (and you actually have tcl installed in your system), then you need to export the paths similarly as suggested by Kamil Czerski in a previous post for virtualenv.
To find out what are your TK and TCL paths, run a python script outside of the venv (source):
import tkinter
root = tkinter.Tk()
print(root.tk.exprstring('$tcl_library'))
print(root.tk.exprstring('$tk_library'))
Open your venv configuration file bin/activate and find the place where they export PATH and insert after this (insert correct paths from step 1):
TCL_LIBRARY="/tcl/path/from/step/1"
TK_LIBRARY="/tk/path/from/step/1"
TKPATH="/tk/path/from/step/1"
export TCL_LIBRARY TK_LIBRARY TKPATH
Deactivate (if it was activated) and source again your venv:
deactivate
source bin/activate
The "Tcl missing"-error should be gone.
IDLE is probably setting the path required for TCL. To find out what path is being used by IDLE, compare the output of sys.path from IDLE and without IDLE. Then you can add the location of init.tcl either using an environment variable or programatically. See Xenomorph suggestion.
All you need to do is copy tcl 8.6 and tcl 8.5 from tcl file to Lib file on in python.
Python-tcl-tcl8.5 to Python-Lib
Go to directory in which all of your python dependencies are stored
Example:
Python37
-DLLs
-Doc
-etc
-include
-Lib
-libs
-Scripts
-tcl
-python.exe
Go to tcl folder, copy the tcl8.5 and tk8.5 folder
Paste these folders in the Lib folder
This solution works for Windows 10 users

winexpect import in a .py not working when running through cmd

Why I am getting this error, when I try to run it through python corenlp.py?
Traceback (most recent call last):
File "corenlp.py", line 23, in (module)
from winpexpect import winspawn
File "C:\Python27\1ib\site-packages\winpexpect-1.5-py2.7.egg\winpexpect.py", 1ine 391, in (module)
class winspawn(spawn):
TypeError: Error when calling the metaclass bases
function() argument 1 must be code, not str
But, When i use it in PythonShell GUI, it is working perfectly.
from winpexpect import winspawn
child = winspawn('java -cp "C:\\Python27\\Scripts\\stanford-corenlp-full-2014-08-27\\*" -Xmx2g edu.stanford.nlp.pipeline.StanfordCoreNLP -annotators tokenize,ssplit,pos,lemma,ner,parse,dcoref -ner.model edu/stanford/nlp/models/ner/english.all.3class.caseless.distsim.crf.ser.gz')
You are getting this error locally because your Python27 doesn't have a prerequisite component installed. In particular it needs Pywin32 installed. You need to download and install Pywin32 from here (specifically the 2.7 version in your case). PythonShell GUI must have this extension already installed so it works.
Finally I was able to resolve my problem(Thanks to #MichaelPetch). I tried this simple example test.py
from winpexpect import winspawn
child=winspawn("java")
It was working fine. Then I realized something wrong with my imports or dependencies. I was using wexpect.py which is another alternative for windows. I renamed it to pexpect.py and copied to my code base.
But winpexpect has dependencies for pexpect(it has another pexpect file on its own folder). When I try to run my .py through cmd it is referring the file in the same folder which is pexpect, I just renamed.
Solution: I just removed wexpect.