1

I am relatively new to the world of Python and using McKinney's book to learn data analysis. My IDE is PyCharm Professional 2018.2

McKinney uses Line Magics to compute elapsed time.

import numpy as np
#Page 87 of 2nd Edition
my_arr = np.arange(1000000)
%time for _ in range(10): my_arr2 = my_arr*2

Now, the issue is that %time generates Unresolved reference error in PyCharm editor, although it runs without any error. As a beginner, I have no clue how to fix this. I have completed all the steps in SO: Unresolved reference issue in PyCharm, PyCharm unresolved reference when importing class from other file by doing this:

a) fixing interpreter - Anaconda 3.6

b) adding project path as source and to sys.path

c) Installing IPython from PyCharm

d) Clear cache and restart -- I have done this three times already......

Please note that when I run %time%... by selecting that line, I don't get an error. Alternatively, if I run this directly in Python Terminal, I don't get an error at all.

Red underlines are very annoying. I'd appreciate if someone could guide me how to fix this.enter image description here

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
watchtower
  • 4,140
  • 14
  • 50
  • 92
  • They are different. `Line Magics` is a future of `IPython`. Mostly, we use `jupyter` to launch an `IPython` notebook. But in pycharm, you can: https://www.jetbrains.com/help/pycharm/using-ipython-notebook-with-product.html – Sraw Aug 02 '18 at 05:36
  • Further, you should understand that `Line Magics` is unavailable in normal Python script. – Sraw Aug 02 '18 at 05:37
  • @Sraw - Thanks for your help. Is there anyway I can fix this? For some reason, I don't like `Jupyter's` command and output in the same window. – watchtower Aug 02 '18 at 05:43
  • Follow the link I give, you can directly run an IPython notebook in Pycharm. Although the output will still be outputted in the same window. That cannot be changed. – Sraw Aug 02 '18 at 05:46
  • @Sraw - Thanks again. I did follow that link and https://www.jetbrains.com/help/pycharm/ipython.html before posting. This didn't help to get rid of those red underlines in my `.py` file. – watchtower Aug 02 '18 at 05:51
  • That's what I've said. They are different. `.ipynb` is IPython notebook while `.py` is normal Python script. You cannot use `Line Magics` in normal Python script but only in IPython notebooks. – Sraw Aug 02 '18 at 05:54

1 Answers1

0

If it runs without an error and you just want to get rid of the red highlighting, you could tell pycharm to ignore the particular "unresolved reference".

  • Go to File | Settings/Preferences | Editor | Inspections - Python - Unresolved references
  • Add reference you wants to ignore.

enter image description here

Note: This will ignore the reference even if you made a legitimate error.

Community
  • 1
  • 1
Nipun Thennakoon
  • 3,586
  • 1
  • 18
  • 26
  • Thanks. Is there anyway to do this systematically without hacking, meaning add an exception? What if I have made a legitimate error? This won't catch such issues. – watchtower Aug 02 '18 at 05:54
  • Not that I know of. Help section about the magic commands in pycharm is pretty vague. It says that pycharm supports usage of IPython magic commands. Try making the interpreter for which IPython has been installed, the default interpreter for the current project. – Nipun Thennakoon Aug 02 '18 at 06:03