私はPythonで非常に大きなプロジェクトを進めています。そして、別の .py ファイルを開くスクリプトを取得する必要があります。私は次のようなスクリプトを使用しようとしました:
os.system("example.py arg")
そして、ファイル内のスクリプトが関数 print などのように小さい場合。しかし、それらが長いか何かの場合、機能しません! したがって、実際に機能し、コマンド プロンプトで .py ファイルを開くスクリプトが必要です。どうもありがとうございました !
を使用してスクリプトを実行する
os.system("example.py arg")
必要
# を付けるべきかどうかも参照してください。(シバン) Python スクリプトで、どのような形式を取る必要がありますか?
別のファイルから関数を取得しようとしている場合は、import
.
from example import my_funct # not that 'example' does NOT have '.py' following it
my_funct() # you can now use the function from the file.
または、スクリプト全体をインポートすることもできます。
import example
example.my_funct() # you have to specify that 'my_funct' is from 'example'
または、次のことができます。
from example import * # this imports all functions from 'example'
my_funct() # now you can use the function as you normally would
注: モジュールから関数をインポートするには、正しく設定する必要があります。これは、関数を に追加する方法ですPYTHONPATH
。