PyCharm: Error while import module (qgis_core) - python

I want to use PyCharm Community Edition 2019.3.1 to write Python based programs for QGIS on Windows 10 64bit. I installed QGIS (v.3.10.1) via the OSGeo4 Installer running with default settings. The implemented Python version is Python 3.7. I created a .cmd script to point on all necessary environments:
SET OSGEO4W_ROOT=C:\OSGeo4W64
call "%OSGEO4W_ROOT%"\bin\o4w_env.bat
call "%OSGEO4W_ROOT%"\apps\grass\grass78\etc\env.bat
#echo off
path %PATH%;%OSGEO4W_ROOT%\apps\qgis\bin
path %PATH%;%OSGEO4W_ROOT%\apps\grass\grass78\lib
path %PATH%;%OSGEO4W_ROOT%\apps\Qt5\bin
path %PATH%;%OSGEO4W_ROOT%\apps\Python37\Scripts
set PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\qgis\python
set PYTHONHOME=%PYTHONHOME%;%OSGEO4W_ROOT%\apps\Python37
start "PyCharm aware of Quantum GIS" /B "C:\Program Files\JetBrains\PyCharm Community Edition 2019.3.1\bin\pycharm64.exe"
After running the script on a fresh installation of PyCharm, the programs opens but also show the error message:
"ImportError: DLL load failed: %1 is not a valid Win32 application."
After accepting this message (This error message didn't appear anymore if I repeating that step)
I created a new Python script and set the missing interpreter on the Python version in the OSGEO4W folder. After updating the Python interpreter I want to import the qgis.core model via:
import qgis.core
but I get following error code:
Traceback (most recent call last):
File "C:/Users/[...]/PycharmProjects/untitled1/venv/aaa", line 1, in <module>
import qgis.core
File "C:\OSGEO4~1\apps\qgis\python\qgis\core\__init__.py", line 25, in <module>
from qgis._core import *
ImportError: DLL load failed: The specified module could not be found.
Process finished with exit code 1,
I'm wondering because the module inclusive functions appears in the auto completion and doesn't throw a error, but after writing the full statement the code greys out and mentioned error occurred.
As a Python newbie I would appreciate any help for my problem!

Related

ModuleNotFoundError: No module named in Python

I keep getting this error in VS Code:
Traceback (most recent call last):
File "c:\Users\User Name\Documents\Productivity\Coding\Python\Udemy\Projects from course\MilestoneP2\app.py", line 1, in <module>
import MilestoneP2.utils.Operations_db as Db
ModuleNotFoundError: No module named 'MilestoneP2'
I have recently shifted from Pycharm to VS Code and I am trying to open some of those projects from pycharm in VS Code but there is the above error haunting me.
PS: I have my Python Interpreter in a different directory and not in the workspace folder. It's in D:\Python\venv Drive
Here is my code:
import MilestoneP2.utils.Operations_db as Db
Here is the file hierarchy.
Anyone Help?
Thank you
If you run the script within VSCode there is a Python version button on bottom-left.
When you click on it you can specify your Python or virtual environment path. It will also try to find them automatically from directories. Then you can run your scripts with the spesified environments.

Script works in Anaconda Prompt but not via Command Line

I am unable to run a python script in the command line, and this script works great in Jupyter Notebook and via Anaconda Prompt. This appears to be an issue importing the ssl module during initialization of another module I am importing (mygeotab).
I have googled the error and done as much as I can to diagnose the most common cause which appears to be PATH issues.
I have already diagnosed the PATH to a point, and have added the location of the /lib/ and python.exe to the environment variables. Also, during testing I began my script with the below to protect myself from path issues and printed the path before and after the 'append', which did not have an impact on the problem.
import sys
print(sys.path)
sys.path.append('C:\\Users\\xxxxxx\\Python Scripts')
sys.path.append('C:\\Anaconda3\\python37.zip')
sys.path.append('C:\\Anaconda3\\DLLs')
sys.path.append('C:\\Anaconda3\\lib')
sys.path.append('C:\\Anaconda3')
sys.path.append('C:\\Anaconda3\\lib\\site-packages')
sys.path.append('C:\\Anaconda3\\lib\\site-packages\\win32')
sys.path.append('C:\\Anaconda3\\lib\\site-packages\\win32\\lib')
sys.path.append('C:\\Anaconda3\\lib\\site-packages\\Pythonwin')
sys.path.append('C:\\Anaconda3\\lib\\site-packages\\IPython\\extensions')
sys.path.append('C:\\Users\\xxxxxx\\.ipython')
sys.path.append('C:\\Anaconda3\\Lib')
sys.path.append('C:\\Anaconda3\\Lib\\site-packages')
print(sys.path)
import mygeotab
import pandas as pd
import pyodbc as py
from mygeotab.ext import feed
import sqlalchemy
from time import sleep
However, when I attempt to run the script via the standard command line, I get the below error:
Traceback (most recent call last):
File "PYTHON_GEOTAB_TRIP_FEED.py", line 33, in <module>
import mygeotab
File "C:\Anaconda3\lib\site-packages\mygeotab\__init__.py", line 9, in <module>
from .api import Credentials
File "C:\Anaconda3\lib\site-packages\mygeotab\api.py", line 14, in <module>
import ssl
File "C:\Anaconda3\lib\ssl.py", line 98, in <module>
import _ssl # if we can't import it, let the error propagate
ImportError: DLL load failed: The specified module could not be found.
The mygeotab module and ssl.py are both in the locations designated in the Traceback. '_ssl' is the only reference I can not seem to diagnose. Again, this works fine in both Notebook and Anaconda Prompt.
Any ideas?
Windows Server 2008 R2
Anaconda 2019.07 x64
Python 3.7.3 x64
This was solved for me by installing a separate instance of Python 3.7, moving the PATH references and other pointers. I installed pip, mygeotab, and the other packages into the native Python 3.7 instance. It just appears you can't use the one baked into anaconda the way I thought. Thanks for the help everyone.

I can import module in python but cannot import the same module when using python command in gdb

I have a simple module called mini. It can be imported in either python interactive interpreter or a python .py script. The module is a C extension based so I want to debug it in gdb (version > 7). I learned that gdb has python command to interpret python command or script like:
(gdb) python import mini
However, it failed to import the module by saying:
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named mini
I guess it might be the environment setting with either my gdb setting or my linux searching path setting. What will be the possible reason of this problem? Thanks.

Import error with remote interpreter in pycharm

I am trying to run my code in the server using ssh remote interpreter.
The connection and the deployment work but when I want to import libraries located in the server it gives an import error
ssh://***#****.com:22/usr/bin/python -u
/home//main.py Traceback
(most recent call last): File
"/home//main.py", line 11,
in
from clplibs.clp import ContinuousLearningPlatform as clp ImportError: No module named clplibs.clp
Process finished with exit code 1
I've found a solution, the environment variables (including python path) should be defined from pycharm: Run/Debug configurations-> Environment variables. Pycharm won't use bashrc paths.

Importing cv in PyDev Console

I have 64 bit python, numpy, and other packages including opencv.
I don't have any problem with following statement in MSDOS console by running python.exe
import cv
But when I did the same thing in PyDev console in Eclipse, I got following error.
import cv
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Python27\lib\site-packages\cv.py", line 1, in <module>
from cv2.cv import *
ImportError: DLL load failed: %1 is not a valid Win32 application.
After some period of trial & error, I found deleting and creating new python interpreter of PyDev solved the problem. More specifically,
Go to Windows -> Preferences -> PyDev -> Python Interpreters
Select current interpreter and click Remove button
Click Auto Config button and select necessary folders for PYTHONPATH
Click Apply

Categories