0

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.

EMichael
  • 95
  • 1
  • 9

1 Answers1

0

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

python3 test.py

Ajinkya
  • 79
  • 1
  • 6
  • python -V output: Python 2.7.8. python3 test.py output: 'python3' is not recognized as an internal or external command, operable program or batch file. – EMichael Feb 27 '19 at 17:35
  • Update: So the script(s) run fine with the command py test.py. But how do I get Python 3 as the default program? I changed it from Properties but still getting the same issue – EMichael Feb 27 '19 at 17:47
  • If you would like to have Python 3.x as a default version, then you will need to create environment variable 'PY_PYTHON' and set it's value to 3. – Ajinkya Feb 27 '19 at 17:52
  • Well, I added the full path under System Variables, which was missing (Python 2.7 was there instead) , created PY_PYTHON under User Variables and restarted. Now python -V shows the right version and running the script with cmd returns no errors. But when I try to open it by double-clicking, instead of staying open and wait for user input it crashes. – EMichael Feb 27 '19 at 17:59
  • This is normal under Windows, you need to put 'input' at the end of your script if you want the Command Prompt window to stay until you hit enter key. Once your console program executes and reaches the end-of-file where you have nothing left, it just exits, this is by default under Windows. Try to click on this script and see what happens: `#myscript.py input("Press enter to exit")` The console window should stay when you click on this script file. – Ajinkya Feb 27 '19 at 18:10
  • please refer [here](https://stackoverflow.com/questions/43965639/double-clicking-foo-py-in-win-explorer-doesnt-work-while-other-running-methods) – Ajinkya Feb 27 '19 at 18:21