Python 3模块导入错误
问题描述:
我创建了一个python 3.4文件并试图将一些变量导入到另一个python脚本中。这两个脚本都在同一个文件夹中,但我不断收到错误。Python 3模块导入错误
我可以使用导入,它工作正常。然而,当我尝试从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()
答
如果使用
from ServerConfiguration import serverSocket, serverIP
你应该只写
serverSocket()
没有ServerConfiguration。
另一种方式:
import ServerConfiguration
...
ServerConfiguration.serverSocket()
你能提供你的源代码片段,错误? – kvorobiev 2015-04-03 08:08:50
我目前正在从Windows控制台运行用户界面 – Alfs 2015-04-03 08:19:02