简体   繁体   English

如何从 python 3.8.0 IDLE shell 运行 python 程序

[英]How to run a python program from python 3.8.0 IDLE shell

I want to launch the program Tip_and_Tax_Calculator.py from a folder named Python27 in a python 3.8.0 IDLE shell but I want to know which code will work.我想从 python 3.8.0 IDLE shell 中名为 Python27 的文件夹中启动程序 Tip_and_Tax_Calculator.py,但我想知道哪些代码可以工作。 I have tried the exec function and execFile but they do not work, I don't know if I am using them in the wrong way too.我试过 exec 函数和 execFile 但它们不起作用,我不知道我是否也以错误的方式使用它们。

import Tip_and_Tax_Calculator
exec('C:\Python27\Tip_and_Tax_Calculator.py')

The error I get is:我得到的错误是:

Traceback (most recent call last): File "C:/Users/Jayan Subramanian/AppData/Local/Programs/Python/Python38-32/Project Launcher.py", line 1, in import Tip_and_Tax_Calculator ModuleNotFoundError: No module named 'Tip_and_Tax_Calculator回溯(最近一次调用):文件“C:/Users/Jayan Subramanian/AppData/Local/Programs/Python/Python38-32/Project Launcher.py”,第 1 行,导入 Tip_and_Tax_Calculator ModuleNotFoundError:没有名为“Tip_and_Tax_Calculator 的模块”

you could use the exec function combined with open and read in the following way for python 3.x :您可以将exec函数与open结合使用,并按以下方式read python 3.x

Notice I change from \\ to / :请注意,我从\\更改为/

exec(open('C:/Python27/Tip_and_Tax_Calculator.py').read())

The method you tried and specified is relevant for python 2.x .您尝试并指定的方法与python 2.x相关。

Another issue, you do not need to import the Tip_and_Tax_Calculator另一个问题,您不需要导入Tip_and_Tax_Calculator

EDIT编辑

after the user added the error message the problem that the script didn't run is the fact the module Tip_and_Tax_Calculator is not in the current shell directory, so one need to specify its full/relative path用户添加错误消息后,脚本未运行的问题是模块Tip_and_Tax_Calculator不在当前 shell 目录中,因此需要指定其完整/相对路径

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

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