Python 为什么我收到无效的语法错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29934449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 05:11:18  来源:igfitidea点击:

Why am I getting an invalid syntax error?

pythonpython-3.xcmdterminalpip

提问by committedandroider

I am following a tutorial on installing the Requests library, a HTTP library for Python. Requests Installation Guide

我正在学习有关安装请求库的教程,这是一个用于 Python 的 HTTP 库。 索取安装指南

The tutorial says to install the Requests library, simply run this command in my terminal

教程说要安装 Requests 库,只需在我的终端中运行此命令

pip install requests

I didn't which terminal to run this command in but I first tried Windows cmd after downloading pip, the package management system used to install and manage software packages written in Python(shown below) enter image description here

我不知道在哪个终端运行这个命令,但我在下载pip后首先尝试了Windows cmd,用于安装和管理用Python编写的软件包的包管理系统(如下所示) 在此处输入图片说明

I then tried the Python 3.4.2 terminal(shown below)

然后我尝试了 Python 3.4.2 终端(如下所示)

enter image description here

在此处输入图片说明

Does anyone know which terminal to run this command in and what my syntax error is for that terminal(tried both)? To me it's weird because the Python terminal was able to recognize pip but not install.....

有谁知道在哪个终端上运行这个命令,以及那个终端的语法错误是什么(两个都试过)?对我来说这很奇怪,因为 Python 终端能够识别 pip 但无法安装.....

采纳答案by dparadis28

You can also create a virtual environment for your project and install all the modules you would like to use. It looks like you are using a windows machine. The commands would be as follows

您还可以为您的项目创建一个虚拟环境并安装您想要使用的所有模块。看起来您正在使用 Windows 机器。命令如下

C:dirrectory_for_your_project>c:\Python34\python.exe c:\Python34\Tools\Scripts\pyvenv.py env  #create your environment
C:dirrectory_for_your_project>env\Scripts\activate.bat   #activate your enviornment
(env) C:dirrectory_for_your_project>pip install requests #pip modules you would like to include in your project
(env) C:dirrectory_for_your_project>python script.py     #run script

There is also a deactivate script for when you would like to exit the env. Make sure to activate the env whenever you are trying to run the script from the command line and all should be fine.

当您想退出 env 时,还有一个停用脚本。确保在尝试从命令行运行脚本时激活 env,一切都应该没问题。

Note: This is just another solution to your problem and is based on personal preference. You should still add the necessary scripts to your path as this might come in handy in the future.

注意:这只是您问题的另一种解决方案,并基于个人喜好。您仍然应该将必要的脚本添加到您的路径中,因为这在将来可能会派上用场。

回答by Tamerz

pip.exe will be in the C:\Python34\Scriptsfolder. You can add that folder to your PATH environment variable if you want to run pip from anywhere, or you can just CD into the Scripts folder from where you are and run it.

pip.exe 将在C:\Python34\Scripts文件夹中。如果您想从任何地方运行 pip,您可以将该文件夹添加到您的 PATH 环境变量中,或者您可以直接从您所在的位置 CD 到 Scripts 文件夹中并运行它。

回答by A.J. Uppal

You are running the pip installfrom your pythonshell. Exit from the pythonshell and run it from your bash.

您正在pip install从您的pythonshell运行。从pythonshell退出并从你的bash运行它。

回答by Antti Haapala

Instead of doing anything with PATHI suggest you use pipon command line with python -mpip, as in python -mpip install somepackage.

PATH我建议你pip在命令行上使用python -mpip,而不是做任何事情,就像在python -mpip install somepackage.