Loonhaunt
Posts: 55
Joined: Sat Nov 16, 2013 5:13 am

importing modules from other directory paths

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]

User avatar
B.Goode
Posts: 8987
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: importing modules from other directory paths

Tue Sep 11, 2018 9:39 am

As a first step, can I recommend moving away from IDLE to an alternative editor/IDE, such as Thonny or Mu.

The IDLE IDE is itself written in python using the tkinter library, and under some circumstances this can cause conflicts and confusion if writing your own code that also uses that library.

That simple step might be enough to solve your problems.

robbes
Posts: 144
Joined: Sun Jan 20, 2013 7:11 pm
Location: Canada - off the west coast

Re: importing modules from other directory paths

Tue Sep 11, 2018 3:57 pm

What is in your variable PYTHONPATH?
Have you made PYTHONPATH available to the python interpreter through an export statement? sys.path won't be able to access it otherwise.

DirkS
Posts: 10006
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: importing modules from other directory paths

Tue Sep 11, 2018 4:02 pm

robbes wrote:
Tue Sep 11, 2018 3:57 pm
What is in your variable PYTHONPATH?
Have you made PYTHONPATH available to the python interpreter through an export statement? sys.path won't be able to access it otherwise.
If you 'properly' install the Adafruit module they would already be in the pythonpath.

I suspect OP just downloaded the module. Either use pip(3) to install it or run the install procedure after downloading.

Loonhaunt
Posts: 55
Joined: Sat Nov 16, 2013 5:13 am

Re: importing modules from other directory paths

Thu Sep 13, 2018 5:01 pm

robbes wrote:
Tue Sep 11, 2018 3:57 pm
What is in your variable PYTHONPATH?
Have you made PYTHONPATH available to the python interpreter through an export statement? sys.path won't be able to access it otherwise.
I do not know exactly how to use a PYTHONPATH variable. I have been unable to find a clear example of it in use. I had been building my code in IDLE. I have recently tried GEANY, kind of "Crashy", no MU. So...I have not made PYTHONPATH available via and Export statement? Perhaps if not too much bother you could recommend an example for me to review?

Dave

DirkS
Posts: 10006
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: importing modules from other directory paths

Thu Sep 13, 2018 5:32 pm

Loonhaunt wrote:
Thu Sep 13, 2018 5:01 pm
I do not know exactly how to use a PYTHONPATH variable.
As I mentioned above you should not need to fiddle around with PYTHONPATH.

Do you use Python 2 or 3?
If you're using Python 2 use

Code: Select all

sudo pip install Adafruit_DHT
For Python 3

Code: Select all

sudo pip3 install Adafruit_DHT
Then try to run your script again from the command line (IDLE should work too, but for 'production' you're probably not going to use IDLE anyway).
Make sure you do not use 'sudo' when starting your script.

Loonhaunt
Posts: 55
Joined: Sat Nov 16, 2013 5:13 am

Re: importing modules from other directory paths

Fri Sep 14, 2018 3:56 pm

That was soooo much better thanks, It seems that the import module Adafruit_DHT imports. When the readTemp Command is invoked the DHT read code runs but I am still erroring out with this:

Traceback (most recent call last):
File "/home/pi/py3prog/TkinterTutor11DHTRead.py", line 5, in <module>
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
NameError: name 'pin' is not defined

Which is progress! However I am naming pin 4 here: pin=4
I also ran from the command line with same result.
I am close but am yet unable to smoke a cigar!

Code: Select all

from tkinter import *
import Adafruit_DHT

sensor=Adafruit_DHT.DHT11
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
pin=4

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!')

User avatar
B.Goode
Posts: 8987
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: importing modules from other directory paths

Fri Sep 14, 2018 4:08 pm

Python is an interpreted language, which means that (apart from loops and conditionals) code is acted on in the order it appears in the script.

So when the interpreter gets to humidity, temperature = Adafruit_DHT.read_retry(sensor, pin) it doesn't know the meaning or value of pin, because you don't set up that variable value until the next line...

Solution: put pin=4 earlier in the script, prior to the humidity, temperature = Adafruit_DHT.... statement.

Loonhaunt
Posts: 55
Joined: Sat Nov 16, 2013 5:13 am

Re: importing modules from other directory paths

Fri Sep 14, 2018 4:36 pm

Of course. Duh! thanks. :D

yfpiLiker
Posts: 32
Joined: Sat Mar 24, 2018 7:46 am

Re: importing modules from other directory paths

Thu Oct 11, 2018 6:39 pm

@Loonhaunt: Please put your code between code tags for better reading.
You can insert them with the Code button on top of the Post reply entry.
Not English. But Pythish.

Return to “Python”