-1

I have installed Python 3.10.6 and Pycharm community edition. Everything was working until I tried to use numpy.

pip3 install numpy
import numpy as np

This is the error message:

    pip3 install numpy
         ^^^^^^^
SyntaxError: invalid syntax

I also have tried to use pip install numpy and pip2 install numpy and pip3 install numpy scipy, but same error. Reinstalling both python and pycharm didn't help.

enter image description here

enter image description here

10
  • pip3 install numpy should be run from your shell, rather from your Python session. Commented Aug 3, 2022 at 15:11
  • you mean from python itself, not from Pycharm? Commented Aug 3, 2022 at 15:20
  • No, from your Command Prompt (in Windows) or bash/zsh etc (in Linux or MacOS): not from Python. You can do this from within PyCharm if you want: use the Terminal tab, rather than the Python Console tab. Commented Aug 3, 2022 at 15:22
  • please check the image that I have added, still the same error Commented Aug 3, 2022 at 15:32
  • You're still in Python in that image (you can tell since it says "Python 3.10.6" at the top and has a >>> prompt). You could either go to the Start Menu in Windows and launch a Command Prompt, or use the Terminal tab in the PyCharm window to start a Command Prompt session. Commented Aug 3, 2022 at 15:36

3 Answers 3

2

Ah, I understand your problem more specifically now. I also use PyCharm, and this same problem happened to me. It was very frustrating, and took me lots of reading to fix it.

PyCharm and other IDEs (integrated development environment) have something called 'run configurations' attached to each file you are working on. These run configurations basically specify which directory on the hard drive the file will use to read and execute your commands. The directory will contain the libraries you need to run your code.

They use these configurations to make it easy to quickly choose which directory (and which libraries) you want a certain file to use. You must specify these configurations in PyCharm for your specific file to run using Numpy. The great thing about PyCharm is that you can actually specify libraries you want to use within the IDE itself (and bypass having to specify a computer-native directory).

Here's How

  1. Go to PyCharm Preferences
  2. Expand the arrow that says 'Project: (your project name)'
  3. Click on 'Python Interpreter'
  4. Click the small '+' symbol
  5. Type in 'numpy' to search for the library (package)
  6. Click install package

Now try to run your file and it should be good to go!

Note that you must do this for each package you wish to use when accessing your file, and as you advance your programming knowledge it will be necessary to learn how to specify the directory you want PyCharm to run the Python Interpreter from. Since you are only using one library though, I think this solution should be fine for the time being.

Sign up to request clarification or add additional context in comments.

Comments

0

You should install numpy with that command in your bash/zsh shell. pip3 install numpy the python script can then import it.

to test, run pip3 install numpy then, python to open a python shell. and then you'll see

>>>

Type import numpy as np and be sure it imports. It should now.

2 Comments

still doesn't work
can you provide the stack trace for the error you got when you installed or when you tried to run the program?
0

It can be maddeningly confusing when first starting out with python and trying to figure out how to download libraries. Here are a few critical things I wish I understood before starting my Python journey, as well as the answer to your question.

  1. Python is the language, and the files that support its functionality are located on the hard drive.

  2. Libraries (like Numpy) can be thought of almost as interpreters (note that we are not using the computer definition of 'interpreter') and are stored alongside the Python files on the hard drive. They give Python more flexibility in terms of what it is able to do by increasing what commands Python is able to understand.

  3. Once a library is downloaded, it must be imported to your Python script before you start writing library-specific commands. Importing a library tells Python: "Hey, I'm going to be writing some commands that you haven't seen before, but here is the library with the commands and what they want you to do in a way that you understand."

  4. 'pip' is Python's installer for these libraries.

Ex) I have a csv file that I want to read. I learn that Pandas has a csv reader function:

pandas.read_csv()

If I were to type this function in a script, Python would have no idea what I meant. But if I were to download Pandas, then import it into my script, Python would understand exactly what I'm saying.

How to Download Numpy

Assuming you are on Windows, open the terminal (command prompt) and run the command: py -m pip install numpy

If you don't already have it, the terminal should have a few lines run and should end with something like 'numpy installed successfully'.

You can check to see if you have it by running the following command in your terminal:

py -m pip list 

This command provides you with a list of all the downloaded libraries. You can check among them to make sure Numpy is downloaded.

Importing Libraries

Once you've downloaded the libraries you need, you need to import them into your script (the Python file where you are writing your code) in order for it to run properly. This is accomplished using the import command. One important thing to note is that you can import libraries and assign them a nickname using the as modifier.

Ex) Back to that csv file I want to read. I don't want to type 'pandas' in front of all the Pandas commands, so when I import it into the script I abbreviate it as 'pd':

import pandas as pd 
pd.read_csv()

See the difference?

TL;DR for Your Scenario

Go to the terminal, and use the py -m pip list command to check if you have Numpy downloaded. If not, run the py -m pip install numpy command. Then go to your script with your actual python code, and import numpy with the import numpy command. Common Python practice is to import numpy as np, FYI.

Hope this clears things up.

It may say that you need to upgrade pip, which is fine, and it should give you a command to run that will upgrade pip to the newest version.

3 Comments

thanks for detailed explanation. Now by typing py -m pip list is can see than numpy and pip and setuptools are installed packages. But when I type import numpy in Pycharm or Command prompt, I still have errors
can you please have a look into pictures I have included?
Yes, gave an answer below. Let me know if you need photos.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.