By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,400 Members | 903 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,400 IT Pros & Developers. It's quick & easy.

run another *.py under a python script

P: n/a
Hope someone can help me, I would like to run another *.py
"test.py" under this def here
Expand|Select|Wrap|Line Numbers
  1. def access_denied(serial_com):
  2.     # This is called when a tag has been scanned and denied
  3.     # Write your own logic here to do more on the computer
  4. LIKE RIGHT HERE
  5.     say('Access Denied')
  6.     # Send the letter D for deny
  7.     serial_com.write('D')
  8.     print 'Tag access denied.'
  9.  
Oct 13 '10 #1
Share this Question
Share on Google+
3 Replies


Expert 100+
P: 622
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.
Oct 13 '10 #2

P: n/a
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:
Expand|Select|Wrap|Line Numbers
  1. #test.py############
  2. import prowlpy
  3.  
  4. apikey = 'xxxxxxxxxxxxxxxxxxx' #Dummy API-key)
  5. p = prowlpy.Prowl(apikey)
  6. try:
  7.     p.add('CatConnect','Cat Outside Door',"!!!!!!!!!!!!")
  8.  
  9. except Exception,msg:
  10.     print msg
  11. #test.py###############################
  12.  
  13. but i really dont get it how i Run that test.py under the man code:
  14.  
  15. ####MAIN.PY##############
  16. #!/usr/bin/env python
  17.  
  18. import sys
  19. import os
  20. import sqlite3
  21. import serial
  22. import subprocess
  23. import test
  24. # Change this to the port displayed in your Arduino IDE
  25. # It will be com3 or something for Windows...
  26. serial_port = 'com3'
  27. baud_rate = 9600
  28.  
  29. # Define the db path to ./rfids.db, relative to this file
  30. sql_file = os.path.join(os.path.dirname(__file__), 'tags.db')
  31. needs_creation = not os.path.exists(sql_file)
  32. db_connection = sqlite3.connect(sql_file)
  33. db_connection.row_factory = sqlite3.Row
  34.  
  35. if needs_creation:
  36.     print 'Creating initial database...'
  37.     cursor = db_connection.cursor()
  38.     cursor.execute("CREATE TABLE rfid_tags (tag_id text, is_enabled integer)")
  39.     db_connection.commit()
  40.     print 'Database created.'
  41.  
  42. def access_denied(serial_com):
  43.     # This is called when a tag has been scanned and denied
  44.     # Write your own logic here to do more on the computer
  45.  
  46. #right here I would like to run the code
  47.  
  48.     say('Cat Tag')
  49.     # Send the letter D for deny
  50.     serial_com.write('D')
  51.  
  52. ################################
Oct 14 '10 #3

P: n/a
got it to work :) a little messy code but works! thx
Oct 14 '10 #4

Post your reply

Sign in to post your reply or Sign up for a free account.