python中main的其他模块无法识别input()

python中main的其他模块无法识别input(),python,python-3.x,input,python-import,main,Python,Python 3.x,Input,Python Import,Main,我有两个脚本,一个是main.py,它通过终端运行并接收input()。和script1.py,它基本上接收文件并解析它 由于导入script1.py(表示变量未定义),我一直收到错误。 错误: 这是script1.py: import os import csv import re import main.document_path open_document= open(document_path) file_name = (os.path.basename(document_path)

我有两个脚本,一个是main.py,它通过终端运行并接收input()。和script1.py,它基本上接收文件并解析它

由于导入script1.py(表示变量未定义),我一直收到错误。 错误:

这是script1.py:

import os
import csv
import re
import main.document_path

open_document= open(document_path)

file_name = (os.path.basename(document_path))
def start_s1(good, bad):
    with open ((('fqdn_') +(str(file_name).rstrip('.csv'))) , 'w') as output:
        with open_document as file:
            fqdn_data = csv.writer(output)
            reader = csv.reader(file)
            good_nums = good
            bad_nums = bad
            maybe_nums = []
            for row in reader:
                if row[3] in good_nums:
                    fqdn_data.writerow([row[2]])
我做错了什么?为了让script1.py能够理解main.py中的变量
document\u path
是什么,应该采取什么措施


非常感谢您的帮助

根据我的评论,您应该重新构造代码,并在模块/函数之间传递documentpath变量

main.py

导入系统 导入操作系统 导入csv 作为pd进口熊猫 也许从进口开始 从script1导入开始\u s1 文档路径=输入(“文件路径是什么?”) 好=输入(“好的数字是什么?”) 坏=输入(“坏数字是什么?”) function1=开始\u s1(文档路径,好,坏) 打印(“已创建FQDN文档!”) maybeasn=开始(好,坏) 打印(“为手动检查创建的目录,文件位于目录中”) 脚本1.py:

import os
import csv
import re
import main.document_path

open_document= open(document_path)

file_name = (os.path.basename(document_path))
def start_s1(good, bad):
    with open ((('fqdn_') +(str(file_name).rstrip('.csv'))) , 'w') as output:
        with open_document as file:
            fqdn_data = csv.writer(output)
            reader = csv.reader(file)
            good_nums = good
            bad_nums = bad
            maybe_nums = []
            for row in reader:
                if row[3] in good_nums:
                    fqdn_data.writerow([row[2]])
导入操作系统
导入csv
进口稀土
def start_s1(文档路径,好,坏):
打开文档=打开(文档路径)
文件名=(os.path.basename(文档路径))
以open(('fqdn')+(str(file_name).rstrip('.csv')),'w')作为输出:
将打开的文档作为文件:
fqdn_data=csv.writer(输出)
reader=csv.reader(文件)
好的
坏的
也许_nums=[]
对于读取器中的行:
如果第[3]行处于良好状态:
fqdn_data.writerow([第[2]行])

虽然@tomgalpin的答案很好,但您也可以在
scrip1.py中使用它:

导入操作系统
导入csv
进口稀土
导入\主\文档\路径
打开文档=打开(文档路径)
文件名=(os.path.basename(文档路径))
def start_s1(良好、不良):
以open(('fqdn')+(str(file_name).rstrip('.csv')),'w')作为输出:
将打开的文档作为文件:
fqdn_data=csv.writer(输出)
reader=csv.reader(文件)
好的
坏的
也许_nums=[]
对于读取器中的行:
如果第[3]行处于良好状态:
fqdn_data.writerow([第[2]行])
main.py
脚本中:

import sys
import os
import csv
import pandas as pd
from maybe import start_maybe 

document_path = input("What is the file path? ")
from script1 import start_s1

open_document = open(document_path) #filelocation 
good = input("What is/are the good numbers? ")
bad = input("What is/are the bad numbers? ")
function1 = start_s1(good,bad)
print ("FQDN document created!")
maybeasn = start_maybe(good,bad)
print("Directory created for manual review, file is located inside")

script.py
中的
\uuuuu main\uuuu
模块是Python的魔法之一。

您可以修改script1.py吗?在Python中,您只能从模块导入模块或函数/类。不能从其他模块导入变量
import main.document\u path
,我会用修改后的code来回答这个问题也许你可以尝试在变量之后进行导入assigment@tomgalpin您可以从其他模块导入变量,而不会出现任何问题。他只需要将其导入从主导入文档路径更改为
否这仍然不正确。。如果结构正确,它将工作,但如上所述,您将在导入中有一个循环。如果您尝试在script1中从main导入任何内容,您将再次执行main,这正是我要推荐的。但是,您不需要更改
start\u s1
的参数,以便将
document\u path
作为第一个参数parameter@tomgalpin谢谢你的帮助和解释。我非常感激。
import sys
import os
import csv
import pandas as pd
from maybe import start_maybe 

document_path = input("What is the file path? ")
from script1 import start_s1

open_document = open(document_path) #filelocation 
good = input("What is/are the good numbers? ")
bad = input("What is/are the bad numbers? ")
function1 = start_s1(good,bad)
print ("FQDN document created!")
maybeasn = start_maybe(good,bad)
print("Directory created for manual review, file is located inside")