question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Numpy & Matplotlib suddenly cannot be imported in Hydrogen although it works fine in the Terminal

See original GitHub issue

Description

Numpy & Matplotlib suddenly cannot be imported in Hydrogen in Atom although it works fine in the Terminal

Steps to Reproduce

I’m regularly importing Numpy and Matplotlib with Hydrogen. They unexpectedly cannot be import for an unknown reason and then appear to be again re-importable, again unexplained.

When trying to import matplotlib with the following command

import matplotlib.pyplot as plt

This scripts runs without problems when I run in a Jupyter notebook or through the Terminal. But When trying to run on Hydrogen in Atom, I get the following error message:

File "<ipython-input-3-3dc8365ef973>", line 6
ModuleNotFoundError: No module named 'matplotlib.backends'; 'matplotlib' is not a package
                             ^
SyntaxError: invalid syntax

I get the same error with Pyside2 when trying to run the line

from PySide2.QtWidgets import QApplication, QLabel

I get the error message

ModuleNotFoundError: No module named 'PySide2.QtWidgets'; 'PySide2' is not a package

Here is the path of python (after having typed which python in the Terminal): /anaconda3/bin/python

I checked that the matplotlib is installed by running the command conda list in the Terminal and got (among all the other packages) those lines:

matplotlib 2.1.2 py36h6d6146d_0
matplotlib 2.2.2 <pip>
PySide2 5.9.0a1.dev1525348214

I tried to run the following command in the Terminal but didn’t solve the problem

sudo apt-get install python3-matplotlib sudo pip3 install matplotlib

I had a similar issue with numpy when trying to import import numpy. Although import numpy was not a problem an hour ago, I get now the following error message (it seems it wants to connect to a previously used script in which numpy was used, to open numpy itself):

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-5-592c151f282b> in <module>()
      1 # from numpy import genfromtxt as gft
----> 2 import numpy

~/Downloads/numpy.py in <module>()
----> 1 from numpy import genfromtxt
      2 my_data = genfromtxt('/Users/mymac/Documents/PyQt/image_viewer/csv_file_generator/eggs.csv',
      3     delimiter=',',
      4     dtype=None,
      5     encoding=None)

ImportError: cannot import name 'genfromtxt'

I had the same issue when tryin to import opencv2: I got the following error message:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
ImportError: numpy.core.multiarray failed to import--------------------------- 
------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-8f6675581547> in <module>()
      5 from PySide2.QtCore import *
      6 from PySide2.QtGui import *
----> 7 import cv2
      8 import numpy
      9 import csv

ImportError: numpy.core.multiarray failed to import

I also enter the following line in the Terminal: atom --clear-window-state but it didn’t help either

I visited the following site which didn’t help me (or I didn’t understand how to implement exactly the steps to fix the problem)

ImportError: No module named matplotlib.pyplot

Importing matplotlib.pyplot in atom editor

matplotlib Error: No module named matplotlib even though it is installed

I also posted

Expected behavior: I want to be able to import package in a python script

Actual behavior: I get a message of the form ModuleNotFoundError: No module named 'matplotlib.backends'; 'matplotlib' is not a package

Reproduces how often: it varies, but it come and goes without a clear time pattern. I also cannot tell why it comes at a certain point. It seems to happen with matplotlib and numpy

Versions

python 3.6.5 macOS 10.13.4 Atom 1.26.1 x64 Hydrogen 2.4.1

Additional Information

When the problem happens, Atom seems to want to connect to a previously used script in which numpy was used, to open numpy itself

Please let me know if something is not clear or if more informations are needed

I first posted this issue on the Github page of Atom https://github.com/atom/atom/issues/17375 but the maintainers told me that it was probably a problem regarding Hydrogen so this is why I’m writing you

Many thanks in advance for your time!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
BenRussertcommented, Jun 16, 2018

Try running conda env list and make sure you have the env you expect active. Right now it looks like you have the root environment active (based on the kernelspec). This means you will need the packages installed within that environment.

Additionally, you will need to make sure the environment you want is active in atom’s environment. If you start atom from the command line (recommended), the env that is active in your terminal should be passed to atom and you will have access to packages installed in that env.

0reactions
bjcecommented, Jun 16, 2018

@BenRussert:

This is the output of conda env list

# conda environments:
#
                         /Users/mymac/.julia/v0.6/Conda/deps/usr
                         /Users/mymac/.platformio/penv
base                  *  /anaconda3

Now I ran

import numpy

I get the error:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-15-d4cdadb62aa7> in <module>()
----> 1 import numpy

~/Downloads/numpy.py in <module>()
----> 1 from numpy import genfromtxt
      2 my_data = genfromtxt('/Users/mymac/Documents/PyQt/image_viewer/csv_file_generator/eggs.csv',
      3     delimiter=',',
      4     dtype=None,
      5     encoding=None)

ImportError: cannot import name 'genfromtxt'

If the error comes from the root environment or the path to it, how can I fix it? shall I change the path? and if yes from where: Atom? Terminal?

Many thanks in advance and sorry for that probably very basic question

Read more comments on GitHub >

github_iconTop Results From Across the Web

Numpy & Matplotlib suddenly cannot be imported in Atom ...
I'm regularly importing Matplotlib Numpy PySide2 in Atom . They unexpectedly cannot be import for an unknown reason and then appear to be ......
Read more >
Matplotlib FAQ
No information is available for this page.
Read more >
Numpy & Matplotlib suddenly cannot be imported in Atom ...
Coding example for the question Numpy & Matplotlib suddenly cannot be imported in Atom although it works fine in the Terminal-numpy.
Read more >
Resolved: Matplotlib figures not showing up or displaying
The issue actually stems from the matplotlib backend not being properly set, or from a missing dependency when compiling and installing ...
Read more >
A Primer on Scientific Programming with Python - IF-UFRJ
and Appendices A–E, you need the NumPy and Matplotlib packages, preferably ... presents programming with while and for loops as well as lists, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found