简体   繁体   English

如何为mac制作等效的.bat以及如何在mac上安装python库

[英]How to make the equivallent of .bat for mac and how to install python libraries on mac

I have this file.py:我有这个file.py:

import os
os.system("pip install pip")
os.system("pip install selenium")

How do I make it work for MAC and what is te equivallent of a.bat file in MAC to execute the file.py.我如何使它适用于 MAC 以及在 MAC 中执行 file.py 的 .bat 文件的等效项是什么。

Your file.py script will generally work fine on Mac as long as the environment the script is running in is set up right.只要脚本运行的环境设置正确,您的file.py脚本通常可以在 Mac 上正常工作。 Most notably, the pip executable has to be findable via the current PATH variable.最值得注意的是, pip可执行文件必须可以通过当前 PATH 变量找到。 You might benefit by looking at the subprocess module, which is an alternative API for running external commands.查看subprocess模块可能会让您受益,它是用于运行外部命令的替代 API。 It is a more robust mechanism for doing so.这是一个更强大的机制。

The equivalent of a.BAT file is a shell script. .BAT 文件的等效项是 shell 脚本。 You have a choice as to which shell to use to run the script.您可以选择使用哪个 shell 来运行脚本。 I think the most common source is the Bash shell.我认为最常见的来源是 Bash shell。 It is often the case that you use whatever shell is running at your command prompt.通常情况下,您使用在命令提示符下运行的任何 shell。 This functionality is generally much more general and flexible than a.BAT file is on Window.此功能通常比 Window 上的 .BAT 文件更通用和灵活。 See this link for a discussion of many of the issues:有关许多问题的讨论,请参见此链接:

https://developer.apple.com/library/archive/documentation/OpenSource/Conceptual/ShellScripting/shell_scripts/shell_scripts.html https://developer.apple.com/library/archive/documentation/OpenSource/Conceptual/ShellScripting/shell_scripts/shell_scripts.html

A shell script can just be one or more commands that you might run in your Terminal. shell 脚本可以只是您可能在终端中运行的一个或多个命令。 For example, to run test.py at a Terminal prompt, you'd do this:例如,要在终端提示符下运行test.py ,您可以这样做:

> python test.py

The simplest equivalent in a shell script would be the same thing: shell 脚本中最简单的等价物是相同的:

python test.py

A script that looks like this is run by whatever shell executes the shell script.看起来像这样的脚本由 shell 执行 shell 脚本运行。 What is more usually done is that a "shebang" line is added to the top of the shell script to explicitly define which shell will be used to run the script.更常见的做法是在 shell 脚本的顶部添加“shebang”行,以明确定义将使用哪个 shell 来运行脚本。 So what the single line script above should really look like is this:所以上面的单行脚本应该是这样的:

#!/bin/sh
python test.py

This may be starting to make your head spin.这可能开始让你头晕目眩。 I would suggest reviewing the link I gave above, and possibly reviewing some other materials that explain shell scripts.我建议查看我上面给出的链接,并可能查看其他一些解释 shell 脚本的材料。 Note that nothing about shell scripts is unique to the Mac.请注意,关于 shell 脚本的任何内容都不是 Mac 独有的。 The concept is exactly the same on Linux, Unix, etc. Linux、Unix等上的概念完全相同。

BTW, do you really want pip install pip ?顺便说一句,你真的想要pip install pip吗? What does that do?那有什么作用? Doesn't the pip package have to already be installed if the pip command is working?如果pip命令有效,是否必须已经安装pip package?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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