简体   繁体   English

如何在另一个 python 文件中运行 Python 文件?

[英]How to run Python file in another python file?

I'm trying to run another python file within this lexical analyzer, but it's not working.我试图在此词法分析器中运行另一个 python 文件,但它不工作。 Can anyone help me?谁能帮我?

I'm using this code: https://github.com/huzaifamaw/Lexical_Analyzer-Parser_Implemented-in-Python/blob/master/main.py我正在使用这段代码: https://github.com/huzaifamaw/Lexical_Analyzer-Parser_Implemented-in-Python/blob/master/main.py

It says I can input my own files by changing blankvar, this is what I wrote:它说我可以通过更改 blankvar 输入我自己的文件,这是我写的:

filecheck = print("INPUT FILE:\n", hello.py)

to run a file called hello.py - to count the lexemes within it.运行一个名为 hello.py 的文件——计算其中的词位。

Can anyone help me?谁能帮我? It'll be much appreciated:)将不胜感激:)

You cannot use hello.py in:你不能在以下地方使用hello.py

filecheck = print("INPUT FILE:\n", hello.py)

If you want to modify the existing program, you have various options.如果你想修改现有的程序,你有多种选择。 The easiest is setting blank_var in this line to your program text.最简单的方法是将此line中的blank_var设置为您的程序文本。

This is what I did and got the following output:这就是我所做的并得到以下 output:

blank_var = "import json\n\ndef foo(): pass\n\ndef bar(n):\n    print(n)\n"

Give this output (not sure about the ERROR IN START message)给这个 output(不确定ERROR IN START消息)

INPUT FILE:
 import json

def foo(): pass

def bar(n):
    print(n)

{'value': 'import', 'LINE_NUMBERS': 1, 'TYPE': 'IDENTIFIER'}
{'value': 'json', 'LINE_NUMBERS': 1, 'TYPE': 'IDENTIFIER'}
{'value': 'def', 'LINE_NUMBERS': 3, 'TYPE': 'IDENTIFIER'}
{'value': 'foo', 'LINE_NUMBERS': 3, 'TYPE': 'IDENTIFIER'}
{'value': '(', 'LINE_NUMBERS': 3, 'TYPE': 'LL_BRACKET'}
{'value': ')', 'LINE_NUMBERS': 3, 'TYPE': 'RL_BRACKET'}
{'value': ':', 'LINE_NUMBERS': 3, 'TYPE': 'COLON'}
{'value': 'pass', 'LINE_NUMBERS': 3, 'TYPE': 'IDENTIFIER'}
{'value': 'def', 'LINE_NUMBERS': 5, 'TYPE': 'IDENTIFIER'}
{'value': 'bar', 'LINE_NUMBERS': 5, 'TYPE': 'IDENTIFIER'}
{'value': '(', 'LINE_NUMBERS': 5, 'TYPE': 'LL_BRACKET'}
{'value': 'n', 'LINE_NUMBERS': 5, 'TYPE': 'IDENTIFIER'}
{'value': ')', 'LINE_NUMBERS': 5, 'TYPE': 'RL_BRACKET'}
{'value': ':', 'LINE_NUMBERS': 5, 'TYPE': 'COLON'}
{'value': 'print', 'LINE_NUMBERS': 6, 'TYPE': 'IDENTIFIER'}
{'value': '(', 'LINE_NUMBERS': 6, 'TYPE': 'LL_BRACKET'}
{'value': 'n', 'LINE_NUMBERS': 6, 'TYPE': 'IDENTIFIER'}
{'value': ')', 'LINE_NUMBERS': 6, 'TYPE': 'RL_BRACKET'}
{'value': '$', 'LINE_NUMBERS': 7, 'TYPE': 'EOF'}
ERROR IN START

You have other options like passing the file path in the arguments or reading from a file location, but this would require some modification as the original program is not readily consumable.您还有其他选项,例如在 arguments 中传递文件路径或从文件位置读取,但这需要进行一些修改,因为原始程序不容易使用。

For example:例如:

with open("my/file.py", "r") as f:
    blank_var = f.read()

So I actually figured out how to get another file to run.所以我实际上想出了如何让另一个文件运行。

For anyone having the same problem as me, here's the code I used:对于和我有同样问题的人,这是我使用的代码:

    file = open("happy.py")
    blank_var= file.read()
    print("INPUT FILE:\n", blank_var)

!!! !!!

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

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