Module not found in PyCharm but externally in Python
Answered
Hi!
I have a kind of curious problem with importing a certain module in PyCharm. I am using PyCharm 2016.3.2
on a Mac. I need the module 'neuron'. When I want to import it via 'import neuron' in PyCharm, it does not find it:
Python 2.7.10 (default, Jul 30 2016, 19:40:32)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
import neuron
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/Applications/PyCharm.app/Contents/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
module = self._system_import(name, *args, **kwargs)
ImportError: No module named neuron
If I launch the interpreter separately in a console, it works:
bg-chl315:~ carl$ python
Python 2.7.10 (default, Jul 30 2016, 19:40:32)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import neuron
NEURON -- VERSION 7.4 (1380:90539e842093) 90539e842093
Duke, Yale, and the BlueBrain Project -- Copyright 1984-2015
See http://www.neuron.yale.edu/neuron/credits
Those are the same Python 2.7 executables. I also invalidated the cache and restarted PyCharm once without effect. Also the module is 'found' to the extent that it shows up in the pull-down list of module propositions:
Any ideas welcome. Thank you!
Please sign in to leave a comment.
I unchecked the following 2 boxes (I couldn't run python commands from pycharm but could from command line so I knew it was a PyCharm thing) But my PyCharm interpreter was same as command line interpreter so I guessed it would be some other setting in PyCharm, and just started changing sh*t, and uncheckign this made it work again.
The below solution worked for me, select option 'Inherit global site-packages'
Please, run this script in Pycharm and in Terminal: https://gist.github.com/vlasovskikh/968240f877117cb6c882 and provide both outputs here. Thank you.
Same here with:
import pandas.
If I run the code from the terminal it's all good but PyCharm does not see it.
PyCharm CE 2017.2.4 on mac os 10.13.2
thanks
I solved it for psutil on Python 3.6 by opening a terminal in PyCharm and installing the development packages:
sudo apt-get install python3.6-dev
Jo
Hi, ok:
PyCharm:
Terminal:
Apparently the PyChar does not have the path to the neuron module in the sys.path variable and in the working_set. When manually adding the path /Applications/NEURON-7.4/nrn/lib/python to the interpreter, the module loads but by program crashes. I just realized that python also crashes if a script based on the neuron module is started externally in the console.
Maybe you're updating your PYTHONPATH variable in your .bashrc file?
Also, if your script crashes when started externally in console, of course, it will crash within PyCharm's console.
I have a similar problem. Trying to run https://pythonhosted.org/pyobjc/examples/core/Scripts/HelloWorld/index.html in PyCharm. It runs fine, but the code has a lot of red underlines, and the code editor can't find the files to import. The first failing import (in the editor) is Foundation, which is in my site-packages.
I have added site-packages to the project structure as source files and marked both that directory and the subdirectory Foundations as source files. It doesn't help.
For a short while, a few times, the editor has found the files to import, but then it forgets it again. It's not something I've been able to reproduce in a consistent way.
I don't see a resolution to this, but have the same problem when trying to load ftfy package:
https://github.com/LuminosoInsight/python-ftfy/issues/92
This seems to be a PyCharm issue. Could someone please attend to this?
UPDATE: I worked around it by creating a Python virtual environment with inherited global site-packages. Now the import seems to load properly.
HiOLEG MELNIKOV! Yes, it looks like PyCharm bug, could you please create an issue in our issue tracker? https://youtrack.jetbrains.com/issues/PY
For whatever it is worth, I am having the same problem.
Note: I am not an expert PyCharm user. Is there a project path I need to update in PyCharm? (I looked but could not find one)
I have the same problem with psutil
PyDev console: starting.
Python 2.7.10 (default, Jul 15 2017, 17:16:57)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin
import psutil
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/Applications/PyCharm Edu.app/Contents/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
module = self._system_import(name, *args, **kwargs)
ImportError: No module named psutil
Any ETA or resolution to the issue?
Having a similar issue:
I think your problem like mine. I tested
import matplotlib
in python shell, it is OK. But shows error in Pycharm. This is because Pycharm don't know the path of site-packages.In your code add these
I think it will help you :)
Another way to add the path to packages without modifying the code is to add it to Interpreter Paths list which can be found in the interpreter settings.
Thank to Uvaisbsc
My issue concern openpyxl and this resolve for me, thanks a lot
I have the same problem, with Django 2.2.7, and Python 3.7.4 for example.
If I start a console from the root directory and launch a shell, it works. If I launch it from the manage.py tool in PyCharm, I get a similar "module not found" error.
I finally made it work simply by
I had also ticked on the 'Always show debug console' on the Console settings, then ticked it off.
My impression is that any change in those settings seems to solve the problem, even if the change is reverted after that. It must be a settings which is not correctly formed until the user triggers a validation when touching a settings in one of those pages (not unlike Aiden3189, who also changed an option there).
Other attempts to restart / invalidate the cache / use another interpreter, or a virtual environment, ... failed. It's very likely an IDE bug, nothing else.
I had the same problem here in PyCharm on mac. I solved it by going in system preferences for pycharm while having the project that I am working on open (namely: UdemyCources) and then Project: UdemyCourses (in my case) then Project Interpreter then clicking on the gear in front of the Project Interpreter box and clicking on add and just clicking ok on the next window in order to get pycharm to update it's packages for the current project. I add the picture and I hope it works for you too.
Uvaisbsc solution worked fine for me.
I often develop parts of the project on an Ubuntu machine (faster) and then copy-paste to RPi4.
And I got into the habit of deleting venv at the target controller and rebuilding it as per instructions quoted by Uvaisbsc .
And then check the list of pakages (as highlighted by Lashkari M J above) to be sure that everything is in place.
I originally had the same problems with "smbus" and GPIO imports
Winfixit's solution works. pip install from pycharm terminal solves the problem.
Hi, the solution of Uvaisbsc is nice, thank you. The problem is that i did not create the project with pycharm but copying existing files to a new directory created by the finder. And i did not have any virtualenv...