Cannot Import MySQLdb in PyCharm, runs fine in terminal. - python

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.

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.

Related

How to integrate ns3 module with pycharm?

I'm new to the ns3, and I was trying to integrate ns3 to PyCharm cause I'm familiar with PyCharm. It seems that ns3 is installed correctly on my machine, running ./waf python works fine but when I use ./waf shell, there are some modules it couldn't find, such as ns.application...etc. So, when I try the solution on this page: add waf libraries to PyCharm, it doesn't work too. Could anyone help me to solve this issue? I would really really appreciate it. By the way, the file I test was the sample from ns3: ns3_root/source/example/tutorial/first.py.
If you have enabled the python bindings and have built them, you need to add the ns3_root/build/bindings/python to the environment variable PYTHONPATH. After that running first.py should work.
You can do that running
export PYTHONPATH=$PYTHONPATH:/path/to/ns3_root/build/bindings/python
Or select first.py, edit the target settings and add that environment variable.
Or if you want this to persist, add this line to the end of the ~/. bashrc file.

Adding Python 2.7 & 3.x Interpreters to PyCharm

I'm having issues adding a project interpreter to PyCharm from a new Anaconda environment. I have Anaconda2 installed with one Python 2.7 environment (C:\Anaconda2\python.exe) that I've been using on Pycharm without issue for several months.
I am attempting to add a second Python 3.6 interpreter (from C:\Anaconda2\envs\py36\python.exe) to my PyCharm. After adding the Local Interpreter to Pycharm, I run into a MS Visual C++ Runtime Error R6034 "An application has made an attempt to load the C runtime library incorrectly".
From cursory googling, it seems that there could be a runtime DLL conflict (potentially msvcr90.dll) between Python 2 & 3. All fixes I see involve editing the executable path of the application, but I don't think this is feasible for my Pycharm use case. How do I get rid of this error, or just generally be able to use both Python 2 & 3 interpreters through my PyCharm?
I think that's the problem with Anaconda and different msvc dll in the computer.
You can test the conda command in the command line, to see if R6034 happens. If it happens, try the following solution:
I had a similar problem with Anaconda3 and Python27. I solved this problem via executing the following command in cmd, outside of any conda environment:
conda install msvc_runtime
After installing the packages, open a new command and test if the R6034 error still appears.
I had a similar issue and was able to resolve it by selecting:
File --> Invalid Caches / Restart...
from PyCharm's main menu.
You may also want to double check that any Conda Environments that you have defined as Python Interpreters in PyCharm are properly configured per the docs
This issue was absolutely maddening. Million R6034 error windows would just keep popping up one after another if I just wanted to get help on a function. I researched it for months, on/off, opened tickets with JetBrains to no avail.
If you need to have multiple versions of Anaconda, and if you have Anaconda paths in your PATH, before launching PyCharm, delete all Anaconda paths from PATH, and then start PyCharm. You need to create a separate wrapper launcher script for PyCharm to fix PATH before PyCharm is started. Note that alternative of starting PyCharm and then fixing interpreter and python console PATHS inside PyCharm do not really work. Because PyCharm may be using a system path to access python to read documentation etc. So the only clean fix is to fix the system PATH before PyCharm starts.
Once you understand what needs to be done, then you can use your own steps/tools. This worked for me:
Create a script that modifies PATH. I used Python for that, sed or any other tools are fine too. The script simply examines each path element and removes it if it refers to Anaconda, and then puts it back together:
path_cleanup.py:
path_old = os.environ['PATH']
path_python_removed = [loc for loc in path_old.split(pathsep) if not ('python' in loc or 'Ana' in loc)]
print(pathsep.join(path_python_removed))
Create Powershell script to fix PATH and start PyCharm from that clean environment. To find PyCharm path, the simplest is to start it up the usual way, and head to Task Manager, right mouse click on pycharm64.exe process and select "open file location" to get the full path.
pycharm_clean.ps1
$Env:Path=python path_cleanup.py # call the script to fix the PATH
start-process $PYCHARM_PATH\pcharm64.exe -WindowStyle Hidden # enter your full path to pycharm and put it into background.
You can create a shortcut to launch pycharm_clean.ps1 + you can add it to your windows start up folder to be launched upon login: %APPDATA%\Microsoft\Windows\Start Menu\Programs\Startup\
If you use BASH inside Cygwin, then steps for path clean up require a bit more tuning, but nothing you cannot do. If you need help, put a comment and I can add that script as well.

calling a package pycharm vs sublime, WindowsOS vs MacOS

I have a project developed in PyCharm and Windows. The project is python based and works fine. The project with the pyCharm editor compiles nicely and works as expected. But when I try to build the project in Sublime/MacOS, It throws out an error - cannot import module.
For an instance, in the attached screenshot you'll see config folder has FrameworkConfig and other scripts. when it is called from Driver.py it throws "import error" in MacOS and/or linux. But works just fine in Windows. What am I missing? Please note that I have properly set up the home directory and is properly pointed.
Take a look at your PYTHONPATH and your sys.path, PyCharm use to add project's path to some of them (not sure which one) automatically, so maybe that's why you can not run it using SublimeText. I have been there too.

in IntelliJ, showing "No module named xxx", but "xxx" is actually installed in my system

My IntelliJ version is 15.0.3, and have python plugin installed.
And when I open a python file in IntelliJ it's like below
Situation here is like below:
when I import these flagged modules in terminal, everything works fine.
running this python file in IntelliJ, is also fine
It's only the red underlying warning annoying me.
I tried this one, this one and this one, but none works for me.
Could anyone please tell me how to get rid of it? Thanks a lot.
You have to add the site-packages path of your interpreter.
For that you have to:
go to Project Structure
choose Global Libraries
choose your Python interpreter
press + at the upper left corner
choose the site-package path of your interpreter
choose "Classes"
also add your python interpreter root directory in the same way.
I have similar problems: cannot import manually installed module. I try to add classpath in Mac environment parameters and intellij SDKs, but it doesn't work. My final solution is to add the classpath of the module to Run/Debug Configurations:
Open Run/Debug Configurations and select your unittest class:
Run --> Edit Configurations...
Add your module's classpath to Environment variables:

Pydev, eclipse and pythonpath problem

I've installed pydev to my eclipse 3.5.2. Everything was working smoothly, create projects, execute, test, autocomplete.
But then I realized that importing modules from /usr/lib/pymodules/python2.6, such as django, causes error "Unresolved import: xxxx". Of course, PYTHONPATH SYSTEM includes the directories I want. What's more, inside package explorer i can c the modules under "System Libs".
I just can't import them :S. Is this a bug? Or I just missing something.
Thanks.
In eclipse you can add django folder in you python path.
Window->Preferences-> PyDev-> Interpreters->Python Interpreter -> Lirararies -> New Folder
And browse till the parent folder of modules you are searching.
If you're using virtualenv you should setup an interpreter using the python build inside.
ie., default python interpreter for th project will be /usr/bin/python
but change it to something like "{project name} python" and point it to your virtual env path. In my case it's ~/.virtualenvs/acme/bin/python
It seems like some sort of cache issue in PyDev... in which case you could try to remove the interpreter, add it again and restart Eclipse.

Categories

Resources