[英]How to run given Python script with class, init and main from cmd?
我提供了帶有 Python 內容的 script.py 文件,其中這是偽代碼:
import pandas as pd
import numpy as np
params = {...}
class SomeClass:
def __init__(self, ab):
self.ab = ab
self.de = None
def main(self):
self.foo()
@staticmethod
def DoSomethingUsefull(abc, params=params):
abc['x'] = abc['Red Fox'].copy()
return some_list(abc);
def foo(self):
self.de = SomeClass.DoSomethingUsefull(self.ab)
self.de['de'] = "de"
我無法更改文件的內容,請按原樣使用。 最終我需要從外部(C#)代碼執行腳本,目前正在嘗試從 cmd (安裝了 Python)運行。 我的問題是如何從命令行執行? 如果無法從命令行執行並且需要包裝腳本,它會是什么樣子?
使用 WinPython 3.9.10
正如@DeepSpace 所說,這個文件只包含一個 class 的定義,它自己不會做任何事情。
您可以創建一個 python 文件來實例化 class 並調用它的一些方法
就像是
import SomeClass
ab = {'Red Fox': 42}
myClass = SomeClass(ab) # instantiate the class
myClass.main() # call the main method.
還請記住,您至少需要修改文件以更正諸如self.de - None
否則您將無法導入 class。
如果您要修改文件以創建拼寫錯誤(是的,我知道您說過您不能修改它,但您必須修改),您可以通過將代碼放在 class 之外來使文件“可執行”,就像這樣:
if __name__ == "__main__":
ab = {'Red Fox': 42}
myClass = SomeClass(ab) # instantiate the class
myClass.main() # call the main method.
這樣你就不需要額外的文件了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.