Script works in Anaconda Prompt but not via Command Line - python

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.

Related

I cannot import libraries into python despite repeatedly installing them

I am struggling to install and import python libraries into my programs. originally I was using pydev but I kept getting messages like:
Traceback (most recent call last): File "C:\Users\satur\eclipse-workspace\DMD experimental\Main.py", line 11, in <module> from matplotlib import pyplot as plot ModuleNotFoundError: No module named 'matplotlib'
I installed matplotlib using py -m pip install matplotlib which produced a few messages like this.
WARNING: The script f2py.exe is installed in 'C:\Users\satur\AppData\Local\Programs\Python\Python310\Scripts' which is not on PATH. Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location. WARNING: The scripts fonttools.exe, pyftmerge.exe, pyftsubset.exe and ttx.exe are installed in 'C:\Users\satur\AppData \Local\Programs\Python\Python310\Scripts' which is not on PATH.
I was unsure what this meant so I tried running my program again and got the module not found error again.
being unable to resolve the issue I moved over to spyder because it came with many of the libraries I wanted thus avoiding the issue. however, as soon as I tried to import and install libraries not included with spyder I encountered the module not found error and a similar warning about the path. I added the paths mentioned in the warning to the PATH in spyder now when I run my code get this error message:
runfile('C:/Users/satur/.spyder-py3/proper orthogonal decomposition/dynamic mode decomposition experimental driver.py', wdir='C:/Users/satur/.spyder-py3/proper orthogonal decomposition')
Traceback (most recent call last):
File "C:\Users\satur\AppData\Local\Programs\Spyder\pkgs\spyder_kernels\py3compat.py", line 356, in compat_exec
exec(code, globals, locals)
File "c:\users\satur\.spyder-py3\proper orthogonal decomposition\dynamic mode decomposition experimental driver.py", line 13, in <module>
import mat73
File "C:\Users\satur\AppData\Local\Programs\Python\Python310\Lib\site-packages\mat73\__init__.py", line 11, in <module>
import h5py
File "C:\Users\satur\AppData\Local\Programs\Python\Python310\Lib\site-packages\h5py\__init__.py", line 25, in <module>
from . import _errors
ImportError: cannot import name '_errors' from partially initialized module 'h5py' (most likely due to a circular import) (C:\Users\satur\AppData\Local\Programs\Python\Python310\Lib\site-packages\h5py\__init__.py)
I did some looking around and found this
https://github.com/spyder-ide/spyder/wiki/Working-with-packages-and-environments-in-Spyder#installing-packages-into-the-same-environment-as-spyder
I followed what instructions I understood
import sys; sys.executable
Out[50]: 'C:\\Users\\satur\\AppData\\Local\\Programs\\Spyder\\Python\\python.exe'
which is different from what I get when I run it in command prompt or in the pydev terminal both of which give me
C:\Users\satur\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\python.exe
when i go to C:\Users\satur\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\ I see: 3 versions of idel, 3 versions of pip, and 6 versions of python.
I am deeply confused. I want to install and import libraries into my python programs. I want to be able to run my programs in either (or for that matter any) ide. I seem to be hitting the very similar issues in both. every tutorial I have found online describes the steps to take to do this but not what they are or why I am doing them none of them seem to work and the more times I try to install things the more confused I get.
could someone please explain: how to install and import libraries into a python program. what python is doing when I install things and try to import them. and why both IDEs have similar but different errors.

PyCharm: Error while import module (qgis_core)

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!

Import errors between some python moduls/functions/variables

This code worked on Friday without problems and still running on a colleagues laptop, but I cannot run it anymore.
As you can see in the screenshot, my editor doesnt find some moduls anymore and the pylint Error "E0401: Unable to import" occurs.
The missing file exists in the folder Settings, as you can see in the Explorer on the left side.
Today I deactivated/activated pylint, reinstalled vs code and python, added the init.py to Settings folder, tried the same code in eclipse, modified the Path enviroment variable and created the PYTHONPATH enviroment variable. All this with no success:/
I am greatful for each hint, which provide me to solve this problem.
The error output as text:
Windows PowerShell
Copyright (C) Microsoft Corporation. Alle Rechte vorbehalten.
PS C:\Users\Evgenij\Desktop\Desktop\Eth_Test_Dev> & C:/Python27/python.exe c:\Users\Evgenij\Desktop\Desktop\Eth_Test_Dev\Code\__TC__Template.py
Traceback (most recent call last):
File "c:\Users\Evgenij\Desktop\Desktop\Eth_Test_Dev\Code\__TC__Template.py", line 36, in <module>
from Lib.IHR_EthApi import EthApi as ETH
File "c:\Users\Evgenij\Desktop\Desktop\Eth_Test_Dev\Code\Lib\IHR_EthApi.py", line 6, in <module>
from IHR_GeneralApi import GeneralApi as SYS
File "c:\Users\Evgenij\Desktop\Desktop\Eth_Test_Dev\Code\Lib\IHR_GeneralApi.py", line 4, in <module>
import IHR_TestSuiteConfig.py
ImportError: No module named IHR_TestSuiteConfig.py
PS C:\Users\Evgenij\Desktop\Desktop\Eth_Test_Dev>
In your code you have the line:
import IHR_TestSuiteConfig.py
That won't work because you don't specify modules to import by file name but by module name, e.g.:
import IHR_TestSuiteConfig
But looking at your screenshot you have a bigger issue of the code being kept in a Settings directory at the same level as your Lib directory containing the code you are importing into.
You need to either anchor all of your code up a level so you can do:
from ..Settings import IHR_TestSuiteConfig
Or you need to manipulate your PYTHONPATH environment variable to put Settings directly on to sys.path (in VS Code you can create a .env file to do this, but it won't' affect running Python from the terminal, only when VS Code runs e.g. Pylint).

Python module sqlite3 cannot be found, but is listed [duplicate]

I would like to have an interface between Python and sqlite. Both are installed on the machine. I had an old version of Python (2.4.3). So, pysqlite was not included by default. First, I tried to solve this problem by installing pysqlite but I did not succeed in this direction. My second attempt to solve the problem was to install a new version of Python. I do not have the root permissions on the machine. So, I installed it locally. The new version of Python is (2.6.2). As far as I know this version should contain pysqlite by default (and now it is called "sqlite3", not "pysqlite2", as before).
However, if I type:
from sqlite3 import *
I get:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/verrtex/opt/lib/python2.6/sqlite3/__init__.py", line 24, in <module>
from dbapi2 import *
File "/home/verrtex/opt/lib/python2.6/sqlite3/dbapi2.py", line 27, in <module>
from _sqlite3 import *
ImportError: No module named _sqlite3
It has to be noted, that the above error message is different from those which I get if I type "from blablabla import *":
Traceback (most recent call last):
File "", line 1, in
ImportError: No module named blablabla
So, python see something related with pysqlite but still has some problems. Can anybody help me, pleas, with that issue?
P.S.
I use CentOS release 5.3 (Final).
On Windows, _sqlite3.pyd resides in C:\Python26\DLLs. On *nix, it should be under a path similar to /usr/lib/python2.6/lib-dynload/_sqlite3.so. Chances are that either you are missing that shared library or your PYTHONPATH is set up incorrectly.
Since you said you did not install as a superuser, it's probably a malformed path; you can manually have Python search a path for _sqlite3.so by doing
import sys
sys.path.append("/path/to/my/libs")
but the preferred approach would probably be to change PYTHONPATH in your .bashrc or other login file.
You have a "slite3.py" (actually its equivalent for a package, sqlite3/__init__.py, so import sqlite3 per se is fine, BUT that module in turns tries to import _sqlite3 and fails, so it's not finding _sqlite3.so. It should be in python2.6/lib-dynload under your local Python root, AND ld should be instructed that it has permission to load dynamic libraries from that directory as well (typically by setting appropriate environment variables e.g. in your .bashrc). Do you have that lib-dynload directory? What's in it? What environment variables do you have which contain the string LD (uppercase), i.e. env|grep LD at your shell prompt?

Why my python does not see pysqlite?

I would like to have an interface between Python and sqlite. Both are installed on the machine. I had an old version of Python (2.4.3). So, pysqlite was not included by default. First, I tried to solve this problem by installing pysqlite but I did not succeed in this direction. My second attempt to solve the problem was to install a new version of Python. I do not have the root permissions on the machine. So, I installed it locally. The new version of Python is (2.6.2). As far as I know this version should contain pysqlite by default (and now it is called "sqlite3", not "pysqlite2", as before).
However, if I type:
from sqlite3 import *
I get:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/verrtex/opt/lib/python2.6/sqlite3/__init__.py", line 24, in <module>
from dbapi2 import *
File "/home/verrtex/opt/lib/python2.6/sqlite3/dbapi2.py", line 27, in <module>
from _sqlite3 import *
ImportError: No module named _sqlite3
It has to be noted, that the above error message is different from those which I get if I type "from blablabla import *":
Traceback (most recent call last):
File "", line 1, in
ImportError: No module named blablabla
So, python see something related with pysqlite but still has some problems. Can anybody help me, pleas, with that issue?
P.S.
I use CentOS release 5.3 (Final).
On Windows, _sqlite3.pyd resides in C:\Python26\DLLs. On *nix, it should be under a path similar to /usr/lib/python2.6/lib-dynload/_sqlite3.so. Chances are that either you are missing that shared library or your PYTHONPATH is set up incorrectly.
Since you said you did not install as a superuser, it's probably a malformed path; you can manually have Python search a path for _sqlite3.so by doing
import sys
sys.path.append("/path/to/my/libs")
but the preferred approach would probably be to change PYTHONPATH in your .bashrc or other login file.
You have a "slite3.py" (actually its equivalent for a package, sqlite3/__init__.py, so import sqlite3 per se is fine, BUT that module in turns tries to import _sqlite3 and fails, so it's not finding _sqlite3.so. It should be in python2.6/lib-dynload under your local Python root, AND ld should be instructed that it has permission to load dynamic libraries from that directory as well (typically by setting appropriate environment variables e.g. in your .bashrc). Do you have that lib-dynload directory? What's in it? What environment variables do you have which contain the string LD (uppercase), i.e. env|grep LD at your shell prompt?

Categories