0

I am writing a python script for an application. The application needs python-mysqldb. In terminal, I can install it with apt-get or pip. How can I install using python script? I am using python 2.7.

1 Answers1

1

You could use pip in a python script for installing the package

import pip

if __name__ == '__main__':
    pip.main(['install', 'MySQL-python'])

Then you can run the script from a terminal.

Timo Hanisch
  • 224
  • 1
  • 7
  • This is equivalent to pip install python-mysqldb? – Janki Chhatbar Apr 09 '15 at 10:29
  • Yes it is. Of course you need pip installed on the machine ;) – Timo Hanisch Apr 09 '15 at 10:33
  • This is giving error.So I treid adding 'if __name__ == '__main__': pip.main(['install','libmysqlclient-dev'])'. This gave error too. So I treid 'if __name__ == '__main__': pip.main(['install','mysql-connector-python'])'. This is giving error too.._Downloading/unpacking mysql-connector-python Could not find any downloads that satisfy the requirement mysql-connector-python Some externally hosted files were ignored (use --allow-external mysql-connector-python to allow)_ – Janki Chhatbar Apr 09 '15 at 11:02
  • So actually, the package you mentioned was not the pip name of the package, I changed it to 'MySQL-python'. – Timo Hanisch Apr 09 '15 at 11:08
  • Yes..tried MySQL-python. It said mysql_config not found. So searched web and it said to __apt-get install libmysqlclient-dev__.tried `if __name__ == __main__: pip.main(['install','libmysqlclient-dev'])`. then tried `if __name__ == '__main__': pip.main(['install',mysql-connector-python ''])`.again error _Could not find any downloads that satisfy the requirement mysql-connector-python Some externally hosted files were ignored (use --allow-external mysql-connector-python to allow)_ – Janki Chhatbar Apr 09 '15 at 11:15
  • Okay, I've found following answer regarding apt-get in python, maybe it helps :) [Answer to apt-get](http://stackoverflow.com/a/8482033/4760822) – Timo Hanisch Apr 09 '15 at 11:26