简体   繁体   中英

How to fix the error that I receive when installing numpy in Python?

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.

在此处输入图像描述

在此处输入图像描述

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM