我创建了一个python3.4文件,并试图将一些变量导入另一个python脚本。这两个脚本都在同一个文件夹中,但我一直收到一个错误。
我可以用进口的,而且很好用。但是,当我尝试使用ServerConfiguration导入端口从脚本导入变量时,ip会出现一个错误: NameError:未定义名称'ServerConfiguration‘
ServerConfiguration模块:
import socket
import sys
import os
serverIP = None
def serverSocket():
PORT = 8884 # Port the server is listening on
ServerEst = input('Has a server been established')
if ServerEst == 'yes':
ServerIP = input ('Enter the servers IP address')
socks = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
socks.bind((ServerIP, PORT))
print('Connection Established at ' + serverIP)
else:
CreateServer = input('Would you like to create a server')
if CreateServer == 'yes':
ServerIP = input('What is you LAN IP? Please remeber if reomte user are allowed to connect port forward port "8884" ')
socks = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
socks.bind((ServerIP, PORT))
print('Connection Established to ' + ServerIP)
else:
print ('Defaulting to default')
ServerIP = 'localhost'
socks = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
socks.bind((ServerIP, PORT))
print('Connection Established to ' + ServerIP)UserModule:
from ServerConfiguration import serverSocket, serverIP
import socket
import sys
import os
def sendMessage():
sockc = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
while True:
MESSAGE = input('Type the message to send ')
sockc.sendto((MESSAGE.encode, "utf-8"), (serverIP, PORT))
print(MESSAGE)
ServerConfiguration.serverSocket()
sendMessage()发布于 2015-04-03 08:18:33
如果您使用
from ServerConfiguration import serverSocket, serverIP 你应该写
serverSocket()没有ServerConfiguration。
另一种方式是:
import ServerConfiguration
...
ServerConfiguration.serverSocket()https://stackoverflow.com/questions/29428524
复制相似问题