简体   繁体   English

在Mac终端上运行Python脚本

[英]Run Python script on Mac terminal

I am trying to execute Python scripts in terminal. 我正在尝试在终端中执行Python脚本。

Running in Python shell it does what it is supposed to. 在Python shell中运行,它可以完成预期的工作。 It will run without error but nothing happens when executed in terminal. 它会无错误运行,但是在终端中执行时什么也不会发生。

Once this is figured out would there be a more useful way to get the program to enter the 'timeAide' & 'cancelSleep' strings into the terminal plus enter Mac password. 一旦解决了这个问题,将有一种更有用的方法来使程序在终端中输入“ timeAide”和“ cancelSleep”字符串,然后输入Mac密码。 I planned on importing 'pyautogui' to get all that portion done but is there something better. 我计划导入“ pyautogui”以完成所有部分,但是还有更好的地方。

#!/usr/bin/env python

#sleepAide: user enters a number to put the computer to sleep
#command for sleep: sudo systemsetup -setcomputersleep 60
#command to cancel sleep: sudo systemsetup -setcomputersleep Never .  

#check python version in terminal: python --version
#shebang line: '#!/usr/bin/python3.6'
#type " 'nano' nameFile.py" in terminal to view code Ex: 'nano namefile.py'

class Sleep(object):
    def __init__(self):
        self.sleepAide()


    def sleepAide(time):                  
        timeAide = 'sudo systemsetup -setcomputersleep '
        cancelSleep = 'sudo systemsetup -setcomputersleep Never'
        time = int(input('In how many minutes would you like to sleep? '))
        if time > 0:
            print(timeAide+' '+str(time))
        elif time == -1:
            print(cancelSleep)

You are only declaring a class and methods. 您只在声明一个类和方法。 You need to instantiate the class for the __init__ function to be called. 您需要实例化要调用__init__函数的类。 You can do so by adding the following at the bottom of the script, outside of the class definition: 为此,您可以在脚本底部的类定义之外添加以下内容:

Sleep()

There a couple of other problems. 还有其他一些问题。

  • You call self.sleepAide() without a time argument, but it doesn't look like you will need it since you collect it via input 您可以不带time参数调用self.sleepAide() ,但是由于您是通过input收集的,因此看起来好像不需要它
  • You do not pass self in the sleepAide definition but try to call it as if it were an instance method 您没有在sleepAide定义中传递self ,而是尝试像调用实例方法一样调用它

I've made a couple changes below to get a working example: 我在下面做了一些更改,以得到一个有效的示例:

class Sleep(object):

    def __init__(self):
        self.sleepAide()

    def sleepAide(self):
        timeAide = 'sudo systemsetup -setcomputersleep '
        cancelSleep = 'sudo systemsetup -setcomputersleep Never'
        time = int(input('In how many minutes would you like to sleep? '))
        if time > 0:
            print(timeAide+' '+str(time))
        elif time == -1:
            print(cancelSleep)


Sleep()

Run with the following: 运行以下命令:

$ python test.py
In how many minutes would you like to sleep? 10
sudo systemsetup -setcomputersleep  10

Keep in mind, this program doesn't actually execute the system commands, it just prints to the console. 请记住,该程序实际上并不执行系统命令,而只是打印到控制台。 If you are looking to execute the commands, this post can help. 如果您希望执行命令, 这篇文章会有所帮助。

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

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