[英]Executing a Python Script From Python Shell
I am trying to access the Amazon Advertising through Python and I created a Python script to automate the authentication process. 我试图通过Python访问亚马逊广告,我创建了一个Python脚本来自动化身份验证过程。 This file, called amazon.py is located in ~/PROJECT/APP/amazon.py. 这个名为amazon.py的文件位于〜/ PROJECT / APP / amazon.py中。
I want to be able to play around with the API, so I launched python manage.py shell from the ~/PROJECT directory to enter the Python shell. 我希望能够使用API,所以我从〜/ PROJECT目录启动了python manage.py shell进入Python shell。 My goal is to be able to execute the python script amazon.py within this shell. 我的目标是能够在这个shell中执行python脚本amazon.py。 What command should I be using to execute amazon.py? 我应该使用什么命令来执行amazon.py?
Normally, you just import the file and call a function within it: 通常,您只需导入文件并调用其中的函数:
import APP.amazon
APP.amazon.main()
This would only work if amazon.py is laid out like this: 这只有在amazon.py布局如下时才有效:
def main():
...code...
if __name__ == '__main__':
main()
Also, in the directory ~/PROJECT/APP there needs to exist a file __init__.py
with nothing in it, or else Python won't see APP as a package with the module amazon in it. 此外,在目录〜/ PROJECT / APP中,需要存在一个文件__init__.py
其中没有任何内容,否则Python将不会将APP视为包含模块亚马逊的包。
Disclaimer: I don't actually know what manage.py does. 免责声明:我实际上并不知道manage.py的作用。
Normally a script is "executed" upon import. 通常在导入时“执行”脚本。 I'd suggest you wrap your functionality in amazon.py
in a function: 我建议你在一个函数amazon.py
你的功能包装在amazon.py
中:
def call_functionality():
...
In your shell you can now import it with: 在shell中,您现在可以使用以下命令导入它:
import path.to.amazon as amazon
and then execute it by 然后执行它
amazon.call_functionality()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.