Tudor Nicolescu
Tudor Nicolescu

Reputation: 39

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

I have this 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.

Upvotes: 0

Views: 546

Answers (1)

CryptoFool
CryptoFool

Reputation: 23079

Your file.py script will generally work fine on Mac as long as the environment the script is running in is set up right. Most notably, the pip executable has to be findable via the current PATH variable. You might benefit by looking at the subprocess module, which is an alternative API for running external commands. It is a more robust mechanism for doing so.

The equivalent of a .BAT file is a shell script. You have a choice as to which shell to use to run the script. I think the most common source is the Bash shell. It is often the case that you use whatever shell is running at your command prompt. This functionality is generally much more general and flexible than a .BAT file is on Window. 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

A shell script can just be one or more commands that you might run in your Terminal. For example, to run test.py at a Terminal prompt, you'd do this:

> python test.py

The simplest equivalent in a shell script would be the same thing:

python test.py

A script that looks like this is run by whatever shell executes the shell script. 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. 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. Note that nothing about shell scripts is unique to the Mac. The concept is exactly the same on Linux, Unix, etc.

BTW, do you really want pip install pip? What does that do? Doesn't the pip package have to already be installed if the pip command is working?

Upvotes: 1

Related Questions