title: Daemon process can't import package
body: <p>I have a TCP server and this server runs a test script called <code>ios_aio.py</code>. If I start the TCP server manually via terminal and run the test everything works, and it works if I launch it via a shell script which looks like this:</p>
  
  <pre><code>#!/bin/bash
  cd /Users/wirelesseasy/Desktop/jiz/Chamber1Utils/servers_DevicesModules
  python  /Users/wirelesseasy/Desktop/jiz/Chamber1Utils/servers_DevicesModules/TCPServer.py
  </code></pre>
  
  <p>In both situations I start the server and the test runs without any problem.</p>
  
  <p>However, if I set the server run as a daemon process, the server still starts but when it loads the test it can't import the package. Is this some permission issue?</p>
  
  <p>The way I run it as daemon is here:</p>
  
  <pre><code>import os, sys
  import getpass
  import subprocess as sp
  
  def find_server(path,server):
      for r, d, f in os.walk(path):
          if server in f:
              return os.path.abspath(r)
  
  def create_agent(cmd,shell):
      import plistlib
      import tempfile
      import datetime
  
      d = { 'Label':'org.wirelesseasy.daemontcp',
            'ProgramArguments':[cmd, shell],
            'RunAtLoad':True,
            'KeepAlive':True
  
            }
  
      output_file = tempfile.NamedTemporaryFile()
      try:
          plistlib.writePlist(d, output_file)
          output_file.seek(0)
          f = open('org.wirelesseasy.daemontcp.plist','w')
          f.write( output_file.read() )
          f.close()
      except:
          raise
      finally:
          output_file.close()
      return os.path.abspath('./org.wirelesseasy.daemontcp.plist')
  
  
  
  
  def create_shell(server_path,cmd='python '):
      sh = open('daemonServer.sh', 'w')
      sh.write('#!/bin/bash\n')
      sh.write('cd '+ server_path + '\n')
  
      sh.write(cmd + ' '+server_path+'/TCPServer.py')
      sh.close()
      shellpath = os.path.abspath('daemonServer.sh')
  
      p = sp.Popen(['chmod','777',shellpath], stdout=sp.PIPE)
  
      while p.poll() is None:
          pass
  
      return os.path.abspath('daemonServer.sh')
  
  
  
  def setagent(agent,dest):
      cmd = 'cp '+ agent +' '+dest
      cmd = cmd.split()
      p = sp.Popen(cmd,stdout=sp.PIPE)
      while p.poll() is None:
          pass
      res = []
      for i in p.stdout:
          res.append(i)
      return res
  
  
  
  def launchagent(dest,agent):
      agent = agent.split('/')
      agent = agent[len(agent)-1]
      # agent = agent.strip('.plist')
      os.chdir(dest)
      cmd = 'launchctl load '+agent
      p = sp.Popen(cmd.split(), stdout=sp.PIPE)
      res = []
      while p.poll() is None:
          pass
      for i in p.stdout:
          res.append(i)
      return res
  
  if __name__ == "__main__":
      cur_dir  = os.path.dirname(os.path.realpath(__file__))
      server_path = find_server(cur_dir+'/..', 'TCPServer.py')
  
      shell = create_shell(server_path)
      agent = create_agent('/bin/sh', shell)
      print agent
  
      user = getpass.getuser()
      dest = '/Users/{}/Library/LaunchAgents'.format(user)
  
      setagent(agent,dest)
      res = launchagent(dest,agent)
      print res
  </code></pre>
  
  <p>The error is simple, it's just can't find those modules the test script imports</p>
  
  <p>The original way is start the TCP server, and in the client, it send command to TCP server, inside the server, it creates a subprocess called ios_aio.py. This works for manually starts the server or using the shell,  but when I use launchctl to set it as daemon, those packages for ios_aio.py can't be imported</p>