title: Python Script runs in PyCharm but Fails in Task Scheduler (Turbodbc)
body: <p>I have a large python script that runs just fine in PyCharm and CMD window but it keeps getting hung up and failing at the same point when I automate it through task scheduler. I have narrowed down the point in the code that it is having issues with but I cannot, for the life of me, figure out why there is a problem. I've even tried building a .bat file for the task scheduler to run but it still fails.</p>
  <p>Code (this is the part that it keeps failing on):</p>
  <pre><code>import time
  import turbodbc as to
  import sys
  
  logfile = &quot;E:\\Python_Scripts\\Report\\log_file.txt&quot;
  
  f = open(logfile, &quot;w&quot;)
  f.write('Start - ' + str(time.ctime(time.time())) + '\n')
  f.close()
  
  # SCA SQL Server Connection
  vns_odbc_driver = '{ODBC Driver 17 for SQL Server}'
  vns_sql_driver = '{SQL Server Native Client 11.0}'
  vns_sql_server = 'SERVER'
  vns_sql_database = 'DATABASE'
  vns_sql_schema = 'dbo'
  vns_sql_username = 'USER'
  vns_sql_password = 'PASS'
  vns_sql_protocol = 'TCPIP'
  vns_sql_port = '1675'
  
  f = open(logfile, &quot;a&quot;)
  f.write(&quot; 1. - &quot; + str(time.ctime(time.time())) + '\n')
  f.close()
  
  try:
      vns_sql_conn = to.connect(
          driver=vns_odbc_driver,  # vns_sql_driver
          server=vns_sql_server,
          database=vns_sql_database,
          uid=vns_sql_username,
          pwd=vns_sql_password,
          protocol=vns_sql_protocol,
          port=vns_sql_port,
          autocommit=True
      )
  except:
      e = sys.exc_info()[0]
      print(e)
      f = open(logfile, &quot;a&quot;)
      f.write(&quot; 2. &quot; + str(e) + ' - ' + str(time.ctime(time.time())) + '\n')
      f.close()
  
  
  f = open(logfile, &quot;a&quot;)
  f.write(&quot; 3.&quot; + ' - ' + str(time.ctime(time.time())) + '\n')
  f.close()
  
  try:
      vns_sql_conn.close()
  except:
      pass
  
  f = open(logfile, &quot;a&quot;)
  f.write(&quot; 4.&quot; + ' - ' + str(time.ctime(time.time())) + '\n')
  f.close()
  </code></pre>
  <p>When I print to a log file I get the following results when run from task scheduler:</p>
  <pre><code>Start - Mon Jun 22 13:03:53 2020
   1. - Mon Jun 22 13:03:53 2020
   2. &lt;class 'turbodbc.exceptions.DatabaseError'&gt; - Mon Jun 22 13:04:09 2020
   3. - Mon Jun 22 13:04:09 2020
   4. - Mon Jun 22 13:04:09 2020
  </code></pre>
  <p>However, When I run it from a CMD window or PyCharm I get this log file:</p>
  <pre><code>Start - Mon Jun 22 13:03:53 2020
   1. - Mon Jun 22 13:03:53 2020
   3. - Mon Jun 22 13:04:09 2020
   4. - Mon Jun 22 13:04:09 2020
  </code></pre>
  <p>Which means it ran fine and connected to the data source.
  I'm thinking it must have something to do with permissions to use the drivers from task scheduler? I am logged into my server with admin privileges and have set up the task with Highest Privileges...</p>
  <p><a href="https://i.stack.imgur.com/oYKL6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/oYKL6.png" alt="Task Scheduler Screenshot" /></a></p>