1

I have problems trying to run a script which imports MySQLdb within PyCharm. Running the script from terminal works just fine while running within PyCharm fails with

ImportError: No module named MySQLdb

I have tried this thread and it helped making things work in the terminal. Trying to set the environment variables in the IDE though does not seem to work. In PyCharm Run Config I set the environment variables

DYLD_LIBRARY_PATH - /usr/local/mysql/lib/libmysqlclient.18.dylib
PATH - /usr/local/mysql/lib/

but I still get the ImportError.

Community
  • 1
  • 1
Alvin
  • 383
  • 5
  • 16
  • One trick with these IDEs is often to start them in an already rightfully prepared environment (that is from terminal) as these settings are magically considered then ... – Dilettant Jun 02 '16 at 11:29
  • I’ve already tried but the problem persists. – Alvin Jun 02 '16 at 11:30
  • Did you see `MySQLdb` in **Project Interpreter**? – qvpham Jun 02 '16 at 11:31
  • Ok the most important path for python when it comes to import search paths is ... `PYTHONPATH` you should inspect that and validate this includes the path to your module (because the error states that there is no such module upon import) – Dilettant Jun 02 '16 at 11:32
  • @julivico you are right. can’t find it in the Project Interpreter. Trying to add it I have mysqldbhelper, raisin.mysqldb, umysqldb... Which one should I pick? Also, I already installed mysqldb using pip, it works fine when calling scripts from the terminal. – Alvin Jun 02 '16 at 11:36
  • @Dilettant you are right, PYTHONPATH does not include a path to mysqldb. But it also does not include paths to other modules that work. Do you suggest I add mysqldb path to it? – Alvin Jun 02 '16 at 11:44
  • 1
    Yes. Other modules might be importable due to some magic in the ide, but the path to mysqldb might be only known to you ;-) please also try Pihl's answer on http://stackoverflow.com/questions/17198319/how-to-configure-custom-pythonpath-with-vm-and-pycharm and maybe skim over http://stackoverflow.com/questions/28326362/pycharm-and-pythonpath ... it might help – Dilettant Jun 02 '16 at 11:47
  • 1
    @Alvin you should choice the right interpreter in **Project Interpreter**. I think, you are using python 2. Try it! P/S: if you want to install, his name is `mysql-python` – qvpham Jun 02 '16 at 12:01

1 Answers1

3

As @Dilettant pointed me to https://stackoverflow.com/a/34992894/1989141, I realised there are two different places I was supposed to set the Python interpreter in PyCharm.

The first is in the main preferences (as in phil’s solution in the link) and allows me to specify a path to a local folder for mysqldb module. The second is in the edit configuration settings for the script I want to run. I noticed the interpreters were different.

The script was set to run with the Python 2.7.6 /System/Library/Frameworks/...... version (which I believe is the OSx pre-installed version). In the terminal and in PyCharm general settings I am using Python 2.7.9 in /usr/local/bin/python.

Matching the script interpreter to the one in the main settings (for which I manyally added mysqldb folder) solves the ImportError. Also I removed the environment variables I set up in the Edit Configuration as they are not needed.

Hope this helps. Thanks to both @Dilettant and @julivico for their suggestions.

Community
  • 1
  • 1
Alvin
  • 383
  • 5
  • 16