[英]Python: can't find '__main__' module in 'C:\\**********\\MP7'
我是 python 的新手。 我正在嘗試在 window cmd 上編譯我的代碼。 當我運行我的 py 文件時,錯誤是:C:\Python\Python38-32\python.exe: can't find ' main ' module in 'C:\Users\tdd24\ECE 615 Vivado\MP7'。 我在 jupyter notebook 中運行了部分代碼,沒有出現這個錯誤。 我找不到問題。 有什么幫助嗎?
import serial
import struct
file1 = open(r'C:\Users\tdd24\ECE 615 Vivado\signal.txt', 'r')
Lines = file1.readlines()
ser = serial.Serial(
port = 'COM4', # /dev/ttyUSB0 in Linux
baudrate=115200,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS
)
# Strips the newline character
for line in Lines:
newpack = struct.pack('f', line)
ser.write(newpack)
#############################
file1.close()
ser.isOpen()
readbyte = 0
readstring = ""
print("Waiting for input")
while (readbyte != '\n'):
readbyte = ser.read(1).decode("utf-8")
readstring = readstring + readbyte
print(readstring)
ser.close()
我在 cmd 上使用的命令
Microsoft Windows [Version 10.0.19042.867]
(c) 2020 Microsoft Corporation. All rights reserved.
C:\Users\tdd24>python --version
Python 3.8.5
C:\Users\tdd24>python "C:\Users\tdd24\ECE 615 Vivado\MP7"
C:\Python\Python38-32\python.exe: can't find '__main__' module in 'C:\\Users\\tdd24\\ECE 615 Vivado\\MP7'
C:\Users\tdd24>
在 windows 上,您需要在執行的.py 中有以下內容
if __name__ == "__main__":
您可以將所有代碼包裝到 function 並從main調用它,也可以將其嵌套在 main 中。 前者可能是更好的選擇,因為它讓您開始對代碼進行分段。
IE
def Function1():
file1 = open(r'C:\Users\tdd24\ECE 615 Vivado\signal.txt', 'r')
Lines = file1.readlines()
#This is your call below, I'm not sure it'll work with comments in the middle, but leaving unchanged to focus on the main point of your query.
ser = serial.Serial(
port = 'COM4', # /dev/ttyUSB0 in Linux
baudrate=115200,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS
)
# Strips the newline character
for line in Lines:
newpack = struct.pack('f', line)
ser.write(newpack)
#############################
file1.close()
ser.isOpen()
readbyte = 0
readstring = ""
print("Waiting for input")
while (readbyte != '\n'):
readbyte = ser.read(1).decode("utf-8")
readstring = readstring + readbyte
print(readstring)
ser.close()
if __name__ == "__main__":
#Starting point for windows execution
Function1()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.