1

I am unable to run NumPy in PyCharm, BUT it works fine from Terminal (Mac OS). How do I get NumPy to work in PyCharm?

This is the error message that I am getting from PyCharm:

Traceback (most recent call last): 
  File "/Users/giridhar/PycharmProjects/numpy/testfile", line 12, 
    in a = np.array([1, 2, 3]) # Create a rank 1 array 
AttributeError: module 'numpy' has no attribute 'array'
bhristov
  • 3,137
  • 2
  • 10
  • 26
  • 1
    Have a look at this: https://stackoverflow.com/questions/36530726/using-numpy-module-object-has-no-attribute-array. Perhaps rename the directory to something else. – cadebe May 23 '20 at 09:04
  • Does this answer your question? [Importing installed package from script raises "AttributeError: module has no attribute" or "ImportError: cannot import name"](https://stackoverflow.com/questions/36250353/importing-installed-package-from-script-raises-attributeerror-module-has-no-at) – David Buck May 23 '20 at 09:40

1 Answers1

1

It looks like the Python that is visible to Pycharm and the system python are different. You will have to specify which python interpreter Pycharm should use.

You can do this the following way:

  1. Press Ctrl+Alt+S to open the project Settings/Preferences.

  2. In the Settings/Preferences dialog Ctrl+Alt+S, select Project {project name} | Project Interpreter. Click the The Configure project interpreter icon and select Add.

You can also try adding this to the top of your Python files:

#!/opt/python3.7/bin/python 

This will allow you to specify which python interpreter should be used from the terminal. Just make sure that you are using a Python interepreter that already has numpy installed.

halfer
  • 19,824
  • 17
  • 99
  • 186
bhristov
  • 3,137
  • 2
  • 10
  • 26
  • I run Mac OS. I see Control and S BUT do not see Alt. I see Project Preferences in Edit Configuration . – Giridhar Tirumalai May 24 '20 at 09:40
  • BtB, I did take a clue from what you had mentioned, and changed the Python path. numpy now works in Pycharm. However, when I run my pure Python code ( in a path different to that of numpy) I get stuck. So, it appears I have to change Python Interpreter path when I run numpy and when I run pure Python code. But then, how can I run a Python code that has numpy code? How do I alter interpreter path? to run python + numpy combined code? Should I just have 1 directory for Python ? – Giridhar Tirumalai May 24 '20 at 10:28
  • 1
    You can try adding this to the top of your python files: #!/opt/python3.7/bin/python This will allow you to specify which python interpreter should be used from the terminal. Just make sure that you are using a python interepreter that already has numpy installed. – bhristov May 26 '20 at 02:04
  • No problem. I am glad that I could help. Could you please mark my answer as the answer to the question? – bhristov May 27 '20 at 04:20
  • Sure, but I am pretty new to Stackoverflow. How do I mark your answer as the one that solved the issue? – Giridhar Tirumalai May 28 '20 at 21:22
  • @GiridharTirumalai Thank you! There is a checkmark under the place where we vote an answer up or down. Please consider upvoting good answers, too. – bhristov May 29 '20 at 03:15
  • I hope I have done what you have asked me to do correctly. Please check and let me know. I see 3 boxes [Active] [Old] [Votes]. I have clicked Votes. – Giridhar Tirumalai May 30 '20 at 11:06
  • @GiridharTirumalai To the left of those there is a label saying 1 Answer. Right below that there are 2 arrows. One going up, for upvoting. One going down, for downvoting. Below this there should be a grey checkmark. Once you click on it it will become green. I hope that this helps. – bhristov May 30 '20 at 18:52
  • Yes, I did click the checkmark - that did turn Green. Hope you can see this .... – Giridhar Tirumalai May 31 '20 at 21:31