2015-04-03 148 views
0

我創建了一個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() 
+0

你能提供你的源代碼片段,錯誤? – kvorobiev 2015-04-03 08:08:50

+0

我目前正在從Windows控制檯運行用戶界面 – Alfs 2015-04-03 08:19:02

回答

2

如果使用

from ServerConfiguration import serverSocket, serverIP 

你應該只寫

serverSocket() 

沒有ServerConfiguration。

另一種方式:

import ServerConfiguration 
... 
ServerConfiguration.serverSocket()