Python Import Error: No module named mysql.connector - python-3.x

I am running into a problem with mysql.connector in Python 3.7 on Windows. I don't know if it's a conflict with the Python 2.7 installation that I did a few days ago. The original script was working fine last time I checked (more than a month ago). The system variables seem fine. Python 3.7 is there.
I isolated the issue to demonstrate better what is going on.
When I attempt to import mysql.connector with IDLE it raises no issues.
IDLE import mysql.connector
Now, with a simple script
import mysql.connector
print("Hello!")
input("Exit.")
again it's fine if I run it with IDLE editor:
Run script with IDLE editor
In fact, the original script runs as intended with IDLE editor.
But, when I attempt to run the script from cmd with 'python test.py' I get this error:
CMD execute script
Of course, when I try to open the script by double-clicking on it, the window opens and closes immediately. I'd appreciate your help on this issue.

First check your python version using python -V if its not 3.7, then use below to execute code from cmd.
python3 test.py

Related

Python command in Terminal works perfectly, but through AppleScript it throws an error

I have a Python script that runs fine through Terminal, but from the moment I try to run the same script with AppleScript (do shell script), I get an error about Python modules that couldn't be found.
More specific:
The script I run refers to a previously installed module, beginning with:
from platform import mac_ver
from Cocoa import NSURL
from CoreFoundation import CFPreferencesAppSynchronize
from CoreFoundation import CFURLCreateWithString
from CoreFoundation import kCFAllocatorDefault
...
While running this in AppleScript, I get an error saying the Cocoa module couldn't be found. I put that line in comment just to test, after which an error about the CoreFoundation module appears. It seems as if the script can't connect with anything that is needed for proper execution.
What could cause this?
Thanks for any info on this!
AppleScript do shell script uses a default shell that has a stripped-down PATH variable. That will make it default to OSX's default Python 2.x install. The easiest solution is to use an explicit path to your install of Python 3.9 (likely something like /usr/bin/python3 or /usr/local/bin/python3).

Why are these import errors occurring when running python scripts from cmd or windows task scheduler, but not anaconda?

I am encoutering import errors, but only when running my python scripts from cmd or windows task scheduler (effectively the same issue I assume). I have researched answers already and attempted various solutions (detailed below), but nothing has worked yet. I need to understand the problem in any case so that I can manage anything like it in the future.
Here is the issue:
Windows 10. Anaconda Python 3.9.7. Virtual enviromnent.
I have a script that works fine if I open an anaconda prompt, activate the virtual environment and run it.
However, this is where the fun starts. If I try to run the script from the non-anaconda cmd prompt deploying the commands: "C:\Users\user\anaconda3\envs\venv\python.exe" "C:\Users\user\scripts\script.py" if get the following error:
ImportError: DLL load failed while importing etree: The specified module could not be found.
Traceback includes:
"C:\Users\user\anaconda3\envs\venv\lib\site-packages\lxml\html\__init__.py", line 53, in <module>
from ..import etree
This is not as simple as one specific module not being installed, because of course running the script from within the anaconda prompt and the virtual environment works. Similar also happens when I run other scripts. Other errors I have seen include, for example:
ImportError: DLL load failed while importing _imaging: The specified module could not be found.
Traceback includes:
"C:\Users\user\anaconda3\envs\venv\lib\site-packages\PIL\Image.py", line 114, in <module>
from . import _imaging as core
Also, I think this may be somehow related. Importing numpy (1.22.3) from within the python interpreter in the virtual environment works fine, but when I try to run a test script that imports numpy it fails both from anaconda and the cmd with the following error:
ImportError: cannot import name SystemRandom
The oveall issue was noted originally when trying to run various scripts from Windows Task Scheduler with the path to python "C:\Users\user\anaconda3\envs\venv\python.exe" entered as the Program/script and the script "script.py" entered as an argument. The above errors were produced, then reproduced by running the scripts from a non-anaconda cmd.
I am looking to understand what is happening here and for a solution that can get the scripts running from the virtual enviroment from Windows Task Scheduler effectively.
Update:
I have uninstalled and reinstalled numpy (and pandas) using conda. This has left the venv with numpy==1.20.3 (and pandas=1.4.2). On attempting to re-run one of the scripts, it runs fine from within the venv in anaconda, but produces the following error when attempting to run from cmd or from within Windows Task Scheduler as above:
ImportError: Unable to import required dependencies:
numpy:
IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!
Importing the numpy C-extensions faled. This error can happen for many reasons, often due to issues with your setup or how NumPy was installed.
We have complied some common reasons and troubleshooting tips at:
https://numpy.org/devdocs/user/troubleshooting-importerror.html
Please note and check the following:
* The Python version is: Python3.9 from "C:\Users\user\anaconda3\envs\venv\python.exe"
* The NumPy version is "1.20.3"
and make sure that they are the versions you expect.
Please carefull study the documentation linked above for further help.
Original error was: DLL load failed while importing _multiarray_umath: The specified module could not be found.
I have looked into the solutions suggested, but am still completely at a loss, especially as to why the script runs from the venv in one place, but NOT the other.

python script fails in terminal via 'import psutil'

I wrote a simple script to check the battery time on my laptop (the built in one on xubuntu seems somewhat inaccurate), the code runs fine in a python terminal but when I run it without starting in the python terminal I get an error related to 'import psutil'.
the script is
I'm very inexperienced with python and haven't been able to find anything helpful online.
#!/usr/bin/env python3.6.7
import psutil
import time
a=psutil.sensors_battery()
b=str(a)
c=b[b.rfind(start)+len(start):b.rfind(end)]
d=int(c)/3600
print(d)
time.sleep(130)
exit()
what I expected is the value c divided by 3600 to be printed on the terminal, display for a while and then exit. instead i get
Desktop$ python battery.py
Traceback (most recent call last):
File "battery.py", line 2, in <module>
import psutil
ImportError: No module named psutil
i also tried running through the menu options when i right click the script and select python3.6, where a terminal flashes and closes immediatly (to quickly to read any of the print in it).
i've also changed
#!/usr/bin/env python3
#!/usr/bin/env python3.6
#!/usr/bin/env python3.6.7
each has the same result
thank you for any advice
david

ImportError: No module named tkinter. Mac OS Terminal

I have created a Tkinter window in a python script. The script runs perfectly in the python IDLE and generates the GUI. However, when I run this file outside of the IDLE by clicking on it on my Desktop, the terminal responds with ImportError: No module named tkinter. Even though tkinter is installed as its a default module when python is installed. Any help is much appreciated. Im working on python 3.7
Make sure your default python , the one that the terminal calls has tkinter installed.
So run 'python --version' in the terminal and if that yields 3.7 then your default python is 3.7.
Now try to import tkinter using that same python version.
open the terminal and run python then run import tkinter as tk
If that does not yield an error saying that the module was not found then most probably you are running the executable with the wrong permissions.

Finding IDLE in Python 3

I am running python 3.3.5. I can't figure out how to launch the IDLE IDE. I installed everything correctly but the IDLE doesn't appear anywhere.
To start it from the command line in Python 3.3+:
$ python3 -midlelib
Or in the code:
import idlelib.PyShell
idlelib.PyShell.main()
You will find an idle.py file at C:\Python33\Lib\idlelib.
There are 2 idle.pys file and both of them work. The first opens a command prompt alongside idle and the other doesn't.

Resources