When calling a python script via the terminal, ImportError - python

When calling a python script from within Pycharm my script runs successfully. However when I call the same script via my terminal I get an import error:
Macs-MacBook:src macuser$ python ./run_events.py
Traceback (most recent call last):
File "./run_events.py", line 3, in <module>
from functions import return_ga_data
File "/Users/macuser/PycharmProjects/ops-google-extract/src/functions.py", line 2, in <module>
import connect
File "/Users/macuser/PycharmProjects/ops-google-extract/src/connect.py", line 4, in <module>
from oauth2client.service_account import ServiceAccountCredentials
ImportError: cannot import name 'ServiceAccountCredentials'
I am not using an environment. Also I'm using python 3.7.
All my python scripts are in the same directory. My terminal's pwd is the same directory.
Tried:
Tried calling the script with python3 ./run_events.py but I get the same result.
Per an SO post about paths I added this to the top of connect.py:
import sys
sys.path.append('/Users/macuser/PycharmProjects/ops-google-extract/src/functions.py')
I still got the same result.
Why can I run the file without an import error from within my IDE but not via the terminal when using ./run_events.py?

Do you have python 2 installed as well? Type python --version in the terminal and see what you get? My guess is Pycharm might be configured to use python 3 while your default python in terminal is python 2, so your python 2 is lacking those modules that was installed for python 3. So when you execute your script in the terminal it's using python 2. If that's the case you can try,
py -3 ./myscript.py

Related

Running python file within a shell script : module not found error

I'm beginner to both bash and python.
I'm working in Ubuntu env.
to be short, I've created a shell script 'format_data.sh' that runs python file 'proc_ko.py' within it.
#!/bin/bash
...
python path/to/python/file/proc_ko.py
...
And the python file 'proc_ko.py' imports a module called khaiii
from khaiii import KhaiiiApi
api = KhaiiiApi()
...
But when I try to execute 'format_data.sh', I get this import error from python file.
Traceback (most recent call last):
File "media/sf_projet/pe/pe/PROGRAMME/SCRIPTS/proc_ko.py", line 5, in
from khaiii import KhaiiiApi
ImportError: No module named khaiii
which doesn't occur when I execute python file independently.
Python file 'proc_ko.py' itself doesn't have any error and 'khaiii' is well installed.
so I don't understand why import error occurs only through the shell script.
If u need more details to figure out, I'll be happy to provide. Thanks in advance for help.

Arbitrary strings in bash throw python errors?

Something in my setup of my shell causes arbitrary strings like "krmpfl" or "u45g5svtJ7" to create a Python error:
$> krmpfl
Traceback (most recent call last):
File "/usr/lib/command-not-found", line 28, in <module>
from CommandNotFound import CommandNotFound
File "/usr/lib/python3/dist-packages/CommandNotFound/CommandNotFound.py", line 19, in <module>
from CommandNotFound.db.db import SqliteDatabase
File "/usr/lib/python3/dist-packages/CommandNotFound/db/db.py", line 5, in <module>
import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'
I would expect bash (and not python!) to throw an error of the kind "Unknown command krmpfl. Did you mean...", but any non-recognized command is for some reason passed to python. I am confused.
Does anyone have an idea on how to debug this or how to move forward? I've tried type krmpfl but this (correctly) echoes bash: type: krmpfl: not found
My setup:
Win10 using Ubuntu 18.04 within WSL
ConEmu as a console
Bash-it
Python 3.8
Click (python package) installed to simplify creating commands
If your current shell function defines a function named command_not_found_handle, bash runs that for a non-existent command rather than immediately failing with a "command not found" error. In your case, that function exists and calls /usr/lib/command-not-found, which appears to be a Python script that tries to download (or at least suggest you download) a package with apt_pkg, but you don't have that Python module installed, which leads to the Python exception.

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 android command throws error while running the command on Python shell

I am trying to execute "import android" command on the python shell. It gives error like this :
>>> **import android**
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
import android
File ".\android.py", line 51
print result['error']
^
I'm having python 3.3.2 installed on the PC (windows 7) and have android.py (SL4A) available in the python directory on my system and the system path also contains python.
Plz help in resolving this error.
Print changed from a statement to a function on Python 3. And the module(android) you're trying to import is written for Python 2.
You should either use a Python 2 interpreter, or find(or make) that module compatible with Python 3.

Categories