0

I've been banging my head over this for many hours and have ravished the internet for answers. I'm afraid I come up dry. This part of my program will detect if Python's MySQL Connector module is installed and if not, will use PIP to install it. There is code in here for both Windows and another. I am trying to get the Windows part working, but for the life of me, can't seem to do it. Here is the code block (forgive any nonsense, I've been stabbing in the dark):

try:
    import mysql.connector as MySQL
except ImportError:
    print("In order to use this program, you need a MySQL connector module,")
    print("provided by MySQL. Do you wish to install this module (uses SUDO)? (y/n)")
    ans = input()
    if ans == "y":
        import subprocess
        if os.name == 'nt': # Windows users will have a different call
            try:
                subprocess.call("pip install mysql-connector-python", shell = True)
            except:
                print("Unable to install module...exiting")
                exit(1)
                raise SystemExit
        else:
            try:
                subprocess.call("sudo pip3 install --allow-external mysql-connector-python mysql-connector-python", shell = True)
            except:
                print("Unable to install module...exiting")
                exit(1)
                raise SystemExit
        print("The MySQL Connector module was unable to be installed...exiting.")
        exit(1)
        raise SystemExit
    else:
        print("The module mysql-connector-python needs to be installed to use this program.")
        print("Module was not installed. Exiting...")
        raise SystemExit

After running the program, here is the output that I get in my console:

Please choose either the 'O' or 'D' option. Print 'H' or 'HELP' for help. Print 'Q' to quit.
-->  d
In order to use this program, you need a MySQL connector module,
provided by MySQL. Do you wish to install this module (uses SUDO)? (y/n)
y
Fatal Python error: Py_Initialize: unable to load the file system codec
  File "C:\Python27\lib\encodings\__init__.py", line 123
    raise CodecRegistryError,\
                            ^
SyntaxError: invalid syntax
The MySQL Connector module was unable to be installed...exiting.

I'm pretty sure that the problem is not with the CodecRegistryError, but with my poor attempt at launching a pip install using 'subprocess' and the Windows Command Prompt. Very much excited for your input!

Update: I am reminded that I forgot to post my system specifications. I am running Eclipse 4.4 (Luna) with the PyDev plugin (3.6.0.201406232321). My operating system is Windows 8.1 Professional 64-bit and I have both Python 2.7 and Python 3.4 installed. My default Python version is 2.7 currently.

midoriha_senpai
  • 177
  • 1
  • 16
  • is this answer for you? [Running windows shell commands with python](http://stackoverflow.com/questions/14894993/running-windows-shell-commands-with-python) – Mathew P. Jones Jul 08 '14 at 06:52
  • I'm sorry, but it did not help. I believe that the issue may be with my specific computing environment. I'm going to try it on another system and see if it remedies the issue. I'll report back. – midoriha_senpai Jul 10 '14 at 16:45
  • Tried it on other Windows computers, to no avail. I set up the program in a Linux environment and it works just fine. It works in a Mac environment too. Thanks for your help though...surprised nobody else chimed in! :-/ – midoriha_senpai Jul 20 '14 at 01:20

1 Answers1

1
#!/usr/bin/env python
from subprocess import check_output
print check_output("dir c:;pwd;", shell=True)
Mathew P. Jones
  • 843
  • 1
  • 11
  • 20
  • I am able to perform a "dir" command on my "C:\" drive, which prints out on the console, not in a separate shell. However, any sort of Python command will not work. I should have mentioned it before, but I am using Eclipse 4.4 (Luna) with the PyDev plugin (3.6.0.201406232321). My operating system is Windows 8.1 Professional 64-bit and I have both Python 2.7 and Python 3.4 installed. My default Python version is currently 2.7. I will add these notes to the main text above as well. Have you come across this before? – midoriha_senpai Jul 10 '14 at 16:40