Splitting program up into multiple files to improve readability











up vote
0
down vote

favorite












I have recently finished a project and I now wish to split up the program into multiple files to improve readability and future improvements to it, however I do not know how to do this. I have split up all functions to do with a certain subject into their own files (database files in a database file, hashing files into hashing, etc etc)



edit: I believe the correct term is "modules" (the 'files' I'm trying to split my program into, so sorry about the lack of correct terminology)



To do this I have done "from foo import *" which I know is bad practice, but in my editor pycharm it does not show any immediate errors unlike doing "import foo".



However, when I run the program, I get errors saying a file I imported is using an undefined variable (which is defined in the main program)
Here is a snippet of the code:



from Data_Funcs import *
if __name__ == "__main__":
DBSetup()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host, port))
s.listen(1)
while True:
conn, addr = s.accept()
connThread = Thread(target=handler, args=(conn, addr))
connThread.daemon = True
connThread.start()


Here is Data_Funcs:



def receiveData(FROM=""):
print("In receive data")
if FROM:
print("From:", FROM)
# sendPickledData(dataReady)
dataString = conn.recv(4096)
try:
dataString = pickle.loads(dataString)
except pickle.UnpicklingError as err:
print("Pickling error:")
print(err)
time.sleep(0.2)
print("Data received:")
print(dataString)
return dataString


def sendPickledData(data):
print("In sendPickled,nSending:",data)
data = pickle.dumps(data)
conn.sendall(data)
time.sleep(0.2)


However when I run the program, I get the error:



  File "C:UsersGreenAppDataLocalProgramsPythonPython36libthreading.py", line 916, in _bootstrap_inner
self.run()
File "C:UsersGreenAppDataLocalProgramsPythonPython36libthreading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "C:/Users/Green/Desktop/Py Proj/Project_Server_Handler_v5.py", line 200, in handler
data = receiveData("handler")
File "C:UsersGreenDesktopPy ProjData_Funcs.py", line 7, in receiveData
dataString = conn.recv(4096)
NameError: name 'conn' is not defined


Despite the variable conn being defined in the "if name == 'main':" part of the main program










share|improve this question


























    up vote
    0
    down vote

    favorite












    I have recently finished a project and I now wish to split up the program into multiple files to improve readability and future improvements to it, however I do not know how to do this. I have split up all functions to do with a certain subject into their own files (database files in a database file, hashing files into hashing, etc etc)



    edit: I believe the correct term is "modules" (the 'files' I'm trying to split my program into, so sorry about the lack of correct terminology)



    To do this I have done "from foo import *" which I know is bad practice, but in my editor pycharm it does not show any immediate errors unlike doing "import foo".



    However, when I run the program, I get errors saying a file I imported is using an undefined variable (which is defined in the main program)
    Here is a snippet of the code:



    from Data_Funcs import *
    if __name__ == "__main__":
    DBSetup()
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    s.bind((host, port))
    s.listen(1)
    while True:
    conn, addr = s.accept()
    connThread = Thread(target=handler, args=(conn, addr))
    connThread.daemon = True
    connThread.start()


    Here is Data_Funcs:



    def receiveData(FROM=""):
    print("In receive data")
    if FROM:
    print("From:", FROM)
    # sendPickledData(dataReady)
    dataString = conn.recv(4096)
    try:
    dataString = pickle.loads(dataString)
    except pickle.UnpicklingError as err:
    print("Pickling error:")
    print(err)
    time.sleep(0.2)
    print("Data received:")
    print(dataString)
    return dataString


    def sendPickledData(data):
    print("In sendPickled,nSending:",data)
    data = pickle.dumps(data)
    conn.sendall(data)
    time.sleep(0.2)


    However when I run the program, I get the error:



      File "C:UsersGreenAppDataLocalProgramsPythonPython36libthreading.py", line 916, in _bootstrap_inner
    self.run()
    File "C:UsersGreenAppDataLocalProgramsPythonPython36libthreading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
    File "C:/Users/Green/Desktop/Py Proj/Project_Server_Handler_v5.py", line 200, in handler
    data = receiveData("handler")
    File "C:UsersGreenDesktopPy ProjData_Funcs.py", line 7, in receiveData
    dataString = conn.recv(4096)
    NameError: name 'conn' is not defined


    Despite the variable conn being defined in the "if name == 'main':" part of the main program










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have recently finished a project and I now wish to split up the program into multiple files to improve readability and future improvements to it, however I do not know how to do this. I have split up all functions to do with a certain subject into their own files (database files in a database file, hashing files into hashing, etc etc)



      edit: I believe the correct term is "modules" (the 'files' I'm trying to split my program into, so sorry about the lack of correct terminology)



      To do this I have done "from foo import *" which I know is bad practice, but in my editor pycharm it does not show any immediate errors unlike doing "import foo".



      However, when I run the program, I get errors saying a file I imported is using an undefined variable (which is defined in the main program)
      Here is a snippet of the code:



      from Data_Funcs import *
      if __name__ == "__main__":
      DBSetup()
      s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
      s.bind((host, port))
      s.listen(1)
      while True:
      conn, addr = s.accept()
      connThread = Thread(target=handler, args=(conn, addr))
      connThread.daemon = True
      connThread.start()


      Here is Data_Funcs:



      def receiveData(FROM=""):
      print("In receive data")
      if FROM:
      print("From:", FROM)
      # sendPickledData(dataReady)
      dataString = conn.recv(4096)
      try:
      dataString = pickle.loads(dataString)
      except pickle.UnpicklingError as err:
      print("Pickling error:")
      print(err)
      time.sleep(0.2)
      print("Data received:")
      print(dataString)
      return dataString


      def sendPickledData(data):
      print("In sendPickled,nSending:",data)
      data = pickle.dumps(data)
      conn.sendall(data)
      time.sleep(0.2)


      However when I run the program, I get the error:



        File "C:UsersGreenAppDataLocalProgramsPythonPython36libthreading.py", line 916, in _bootstrap_inner
      self.run()
      File "C:UsersGreenAppDataLocalProgramsPythonPython36libthreading.py", line 864, in run
      self._target(*self._args, **self._kwargs)
      File "C:/Users/Green/Desktop/Py Proj/Project_Server_Handler_v5.py", line 200, in handler
      data = receiveData("handler")
      File "C:UsersGreenDesktopPy ProjData_Funcs.py", line 7, in receiveData
      dataString = conn.recv(4096)
      NameError: name 'conn' is not defined


      Despite the variable conn being defined in the "if name == 'main':" part of the main program










      share|improve this question













      I have recently finished a project and I now wish to split up the program into multiple files to improve readability and future improvements to it, however I do not know how to do this. I have split up all functions to do with a certain subject into their own files (database files in a database file, hashing files into hashing, etc etc)



      edit: I believe the correct term is "modules" (the 'files' I'm trying to split my program into, so sorry about the lack of correct terminology)



      To do this I have done "from foo import *" which I know is bad practice, but in my editor pycharm it does not show any immediate errors unlike doing "import foo".



      However, when I run the program, I get errors saying a file I imported is using an undefined variable (which is defined in the main program)
      Here is a snippet of the code:



      from Data_Funcs import *
      if __name__ == "__main__":
      DBSetup()
      s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
      s.bind((host, port))
      s.listen(1)
      while True:
      conn, addr = s.accept()
      connThread = Thread(target=handler, args=(conn, addr))
      connThread.daemon = True
      connThread.start()


      Here is Data_Funcs:



      def receiveData(FROM=""):
      print("In receive data")
      if FROM:
      print("From:", FROM)
      # sendPickledData(dataReady)
      dataString = conn.recv(4096)
      try:
      dataString = pickle.loads(dataString)
      except pickle.UnpicklingError as err:
      print("Pickling error:")
      print(err)
      time.sleep(0.2)
      print("Data received:")
      print(dataString)
      return dataString


      def sendPickledData(data):
      print("In sendPickled,nSending:",data)
      data = pickle.dumps(data)
      conn.sendall(data)
      time.sleep(0.2)


      However when I run the program, I get the error:



        File "C:UsersGreenAppDataLocalProgramsPythonPython36libthreading.py", line 916, in _bootstrap_inner
      self.run()
      File "C:UsersGreenAppDataLocalProgramsPythonPython36libthreading.py", line 864, in run
      self._target(*self._args, **self._kwargs)
      File "C:/Users/Green/Desktop/Py Proj/Project_Server_Handler_v5.py", line 200, in handler
      data = receiveData("handler")
      File "C:UsersGreenDesktopPy ProjData_Funcs.py", line 7, in receiveData
      dataString = conn.recv(4096)
      NameError: name 'conn' is not defined


      Despite the variable conn being defined in the "if name == 'main':" part of the main program







      python-3.x import module






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 at 0:29









      James Green

      407




      407





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244754%2fsplitting-program-up-into-multiple-files-to-improve-readability%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244754%2fsplitting-program-up-into-multiple-files-to-improve-readability%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Full-time equivalent

          さくらももこ

          13 indicted, 8 arrested in Calif. drug cartel investigation