Search code examples
pythonpackageanacondapython-idle

Modules I've installed with pip and conda are not importable in Sublime or Atom, but can be imported using Jupyter Notebook (and sometimes Terminal)


The problem:
When I run the following code using Atom or Sublime:

import quandl
df = quandl.get('WIKI/GOOGL')
print(df)

I get the following error:

Traceback (most recent call last):
File "/Users/patrick/Desktop/Untitled.py", line 1, in <module>
import quandl
ImportError: No module named quandl
[Finished in 0.3s with exit code 1]
[shell_cmd: python -u "/Users/patrick/Desktop/Untitled.py"]
[dir: /Users/patrick/Desktop]
[path: /Library/Frameworks/Python.framework/Versions/3.7/bin:/anaconda3/bin:/anaconda3/condabin:/Library/Frameworks/Python.framework/Versions/3.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin]

*I removed my last name wherever it appeared in the output.
Yet when I run that same code in Terminal (the file is named Untitled.py) via the following code:

$ cd Desktop
$ python Untitled.py

I get a data frame printed (as desired), so quandl was apparently imported. Another potentially helpful fact is that when I tried to install nibabel via pip install nibabel, it seemed to install. But when I wrote a program that simply said import nibabel it was not importable using Terminal or Sublime. Here's the error code in Terminal:

Traceback (most recent call last):
  File "Untitled.py", line 1, in <module>
    import nibabel
ModuleNotFoundError: No module named 'nibabel'

And here was the error code in Sublime (last name removed):

Traceback (most recent call last):
  File "/Users/patrick/Desktop/Untitled.py", line 1, in <module>
    import nibabel
ImportError: No module named nibabel
[Finished in 0.3s with exit code 1]
[shell_cmd: python -u "/Users/patrick/Desktop/Untitled.py"]
[dir: /Users/patrick/Desktop]
[path: /Library/Frameworks/Python.framework/Versions/3.7/bin:/anaconda3/bin:/anaconda3/condabin:/Library/Frameworks/Python.framework/Versions/3.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin]

However, when I opened up Anaconda Navigator, selected Jupyter Notebook, and then ran the same code In [1]: import nibabel it worked and didn't throw any errors.

In summary, importing quandl with pip and conda allowed me to use Jupyter Notebook and Terminal but not Atom or Sublime. However, importing nibabel with pip only (it's not available from conda) allowed me to import it in Jupyter Notebook, but not Terminal, Atom, or Sublime.

Forum posts that seem relevant & solutions I've tried:
I found what appears to be a Windows version of my problem, but I wasn't sure how to adapt the solution to the Mac file system. @Biker_Coder (Mani) seemed to think the problem was with the path that python looks for packages in. Windows version of my problem
There are a few other forum posts with similar looking problems and some people suggested that the problem might be that the author was running two versions of Python. That doesn't sound like my problem because I got started pretty recently with Python and don't think I downloaded two versions of it. In a few other forums, people have had luck changing "quandl" to "Quandl" or vice versa. This didn't work for me. In fact, I'm pretty sure it has nothing to do with quandl because installing and importing nibabel lead to the same problems.
THANKS to everyone who read all this, whether or not you know the solution.


Potentially Useful System Information: That's the entirety of my question, but here's some additional information in case it's helpful. When I open IDLE and run >>> import sys and then >>>print(sys.path), I get the following output (again I removed my last name wherever it appeared):

['', '/Users/patrick/Documents', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python37.zip', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages']

When I type $ python --version into Terminal, it outputs Python 3.7.2 My computer is running "MacOS Mojave Version 10.14.2".


Solution

  • It seems that you're using conda in Jupyter and Atom, but not in your terminal. If you run conda env list, you will get a list of conda environments. By default, it uses base and by looking at your PATH, that's what's used in your editor.

    To enter that environment in terminal, run conda activate base. After that, run pip install nibabel (or conda install nibabel). You should be able to run your script from terminal and use the library running from Atom.

    Btw it seems that your conda installation hasn't really changed your .bash_rc file since it's not loading base environment by default.