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.

Examples missing import numpy as np?

See original GitHub issue

Hi

a bunch of examples seem to be missing import numpy as np. At least for me they throw NameError: name 'np' is not defined.

Following examples seem to be missing import numpy as np (non exhaustive):

  • scipy.signal.spectrogram
  • scipy.signal.periodogram
  • scipy.signal.welch

scipy.__version__ gives 1.4.1. However, the examples seem to be the same in the current master.

Maybe I am missing something? Any help is appreciated! Thanks 😃

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:48 (43 by maintainers)

github_iconTop GitHub Comments

4reactions
stefanvcommented, Jun 10, 2022

I don’t have a horse in this race, but I wanted to mention an alternative that came up on https://github.com/networkx/networkx/issues/5720

We could ask IPython/Jupyter whether they could detect common missing names and give an error message after a NameError with some guidance, such as:

Hint: it looks like you are missing an import; to use SciPy, try `import scipy as sp` first

This is a best-of-both-worlds type scenario, where we don’t need to repeat the same imports over and over in the docstrings, but users get helpful feedback for common package imports (np, sp, plt, pd, nx, etc).

4reactions
andyfaffcommented, May 11, 2021

Some docstrings have multiple examples, but these are often interspersed with lots of text. Should a numpy import happen once in a docstring, or with each example (as previous comments suggest)? The latter is bloat-worthy.

numpy is ubiquitous, so knowing what np.sum means without the import being included in a docstring doesn’t seem like an egregious act. Even then it’s only a websearch away. A single websearch is small fry compared to ‘ugliness’ caused by adding an explicit numpy import.

Besides, there are inline code snippets in multiple places that implicitly use np: np.std(pop) <= atol + tol * np.abs(np.mean(population_energies)) is present in the docstring, do they deserve an import?

In a typical interpreter session (Python/IPython) you only need to do the import once (18 characters worth), and it lasts for the duration of the session. I would argue that the cost of typing that is miniscule compared to the cost incurred by adding the numpy import to every example.

Read more comments on GitHub >

github_iconTop Results From Across the Web

import NumPy as np ImportError No module named ... - Edureka
Hi Guys,. I have NumPy installed in my computer. But still I am getting this below error. import NumPy as np ImportError: No...
Read more >
ModuleNotFoundError: No module named numpy as np
I am using Visual Studio Code and try to 'import cv2', but I see an error.
Read more >
How to Fix: No module named NumPy - GeeksforGeeks
In this article, we will discuss how to fix the No module named numpy using Python. Numpy is a module used for array...
Read more >
How to Fix: No module named numpy - Statology
This error occurs when Python does not detect the NumPy library in your current environment. This tutorial shares the exact steps you can...
Read more >
[Fixed] ModuleNotFoundError: No module named 'numpy'
Example : Suppose that you are trying to import the Numpy library and print an array. However you get an ImportError: ModuleNotFoundError: No...
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