run another *.py under a python script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dennis Thyboron

    run another *.py under a python script

    Hope someone can help me, I would like to run another *.py
    "test.py" under this def here
    Code:
    def access_denied(serial_com):
        # This is called when a tag has been scanned and denied
        # Write your own logic here to do more on the computer
    LIKE RIGHT HERE
        say('Access Denied')
        # Send the letter D for deny
        serial_com.write('D')
        print 'Tag access denied.'
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    You would use "import" to use the methods of test.py in your current script. Effbot's writeup of import and an example of using import. Note that the directory must be in sys.path so you may have to add the directory containing the program to sys.path first.

    Comment

    • Dennis Thyboron

      #3
      I am really a noob at this :(
      I can't get it work,, this code I am trying to run inside the main code is this:
      Code:
      #test.py############
      import prowlpy
      
      apikey = 'xxxxxxxxxxxxxxxxxxx' #Dummy API-key)
      p = prowlpy.Prowl(apikey)
      try:
          p.add('CatConnect','Cat Outside Door',"!!!!!!!!!!!!")
          
      except Exception,msg:
          print msg
      #test.py###############################
      
      but i really dont get it how i Run that test.py under the man code:
      
      ####MAIN.PY##############
      #!/usr/bin/env python
      
      import sys
      import os
      import sqlite3
      import serial
      import subprocess
      import test
      # Change this to the port displayed in your Arduino IDE
      # It will be com3 or something for Windows...
      serial_port = 'com3'
      baud_rate = 9600
      
      # Define the db path to ./rfids.db, relative to this file
      sql_file = os.path.join(os.path.dirname(__file__), 'tags.db')
      needs_creation = not os.path.exists(sql_file)
      db_connection = sqlite3.connect(sql_file)
      db_connection.row_factory = sqlite3.Row
      
      if needs_creation:
          print 'Creating initial database...'
          cursor = db_connection.cursor()
          cursor.execute("CREATE TABLE rfid_tags (tag_id text, is_enabled integer)")
          db_connection.commit()
          print 'Database created.'
          
      def access_denied(serial_com):
          # This is called when a tag has been scanned and denied
          # Write your own logic here to do more on the computer
      
      #right here I would like to run the code
      
          say('Cat Tag')
          # Send the letter D for deny
          serial_com.write('D')
          
      ################################
      Last edited by bvdet; Oct 14 '10, 06:50 PM. Reason: Please use code tags when posting code [code]....code goes here....[/code]

      Comment

      • Dennis Thyboron

        #4
        got it to work :) a little messy code but works! thx

        Comment

        Working...