importing modules from other directory paths
Posted: Tue Sep 11, 2018 9:24 am
I am working with a bit of code from a tutorial and I do not know the proper means to import modules from other directories that the one that Iam running the code from i.e. This bit errors out with " No Adafruit_DHT module" when I run straight from IDLE.. If I move the code to /Adafruit_DHT/example, where the module should exist I error out with "No tkinter module" etc. I have searched for an example of a PYTHONPATH or sys.path (The Definitive Guide to Module Importing) but am unable to come up with a combination that works? If someone could point me at a better source for accomplishing module imports across directories I would appreciate the help.
Thanks,
from tkinter import *
import Adafruit_DHT
def doNothing():
print("Okay I will Not,,,")
def readTemp():
if humidity is not None and Temperature is not None:
print('Temp={0:0.1f}*C Humidity={1:0.1f}%'.format(temperature*9/5+32,humidity))
else:
print('Failed to get reading. Try again!')
root = Tk()
sensor=Adafruit_DHT.DHT11
pin=4
menu = Menu(root)
root.config(menu=menu)
subMenu = Menu(menu)
menu.add_cascade(label="File",menu=subMenu)
subMenu.add_command(label="New Project...", command=readTemp)
subMenu.add_command(label="New...", command=doNothing)
subMenu.add_separator()
subMenu.add_command(label="Exit", command=doNothing)
editMenu=Menu(menu)
menu.add_cascade(label="Edit", menu=editMenu)
editMenu.add_command(label="Redo",command=doNothing)
#.......Toolbar.....
toolbar=Frame(root,bg="blue")
insertButt=Button(toolbar,text="Insert Image",command=doNothing)
insertButt.pack(side=LEFT,padx=2,pady=2)
printButt=Button(toolbar,text="Print",command=doNothing)
printButt.pack(side=LEFT,padx=2,pady=2)
toolbar.pack(side=TOP,fill=X)
#.......Status Bar..................
status=Label(root,text="Preparing to do Nothing...",bd=1,relief=SUNKEN,anchor=W)
status.pack(side=BOTTOM,fill=X)
root.mainloop()[/code]
Thanks,
from tkinter import *
import Adafruit_DHT
def doNothing():
print("Okay I will Not,,,")
def readTemp():
if humidity is not None and Temperature is not None:
print('Temp={0:0.1f}*C Humidity={1:0.1f}%'.format(temperature*9/5+32,humidity))
else:
print('Failed to get reading. Try again!')
root = Tk()
sensor=Adafruit_DHT.DHT11
pin=4
menu = Menu(root)
root.config(menu=menu)
subMenu = Menu(menu)
menu.add_cascade(label="File",menu=subMenu)
subMenu.add_command(label="New Project...", command=readTemp)
subMenu.add_command(label="New...", command=doNothing)
subMenu.add_separator()
subMenu.add_command(label="Exit", command=doNothing)
editMenu=Menu(menu)
menu.add_cascade(label="Edit", menu=editMenu)
editMenu.add_command(label="Redo",command=doNothing)
#.......Toolbar.....
toolbar=Frame(root,bg="blue")
insertButt=Button(toolbar,text="Insert Image",command=doNothing)
insertButt.pack(side=LEFT,padx=2,pady=2)
printButt=Button(toolbar,text="Print",command=doNothing)
printButt.pack(side=LEFT,padx=2,pady=2)
toolbar.pack(side=TOP,fill=X)
#.......Status Bar..................
status=Label(root,text="Preparing to do Nothing...",bd=1,relief=SUNKEN,anchor=W)
status.pack(side=BOTTOM,fill=X)
root.mainloop()[/code]