Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to set as a module in python 2.7 #8

Open
happy-jo opened this issue Feb 19, 2016 · 4 comments
Open

Unable to set as a module in python 2.7 #8

happy-jo opened this issue Feb 19, 2016 · 4 comments

Comments

@happy-jo
Copy link

I'm new to coding but kinda fell in love.

So I have a python script that posts a var to IO.adafruit.com. I have it working with a DHT11.
I'm trying to transition to an i2c sensor like the sht21 or sht31. But because the sht#1 code is long, I'm trying to make a module of it that I can import into my main code or to any other.

So I copied the sht31.py and sht21.py into /usr/lib/python2.7/ and /usr/lib/python3.4. I should be able to import the module into my code.

In the interpreter, I am able to import the sht31 or sht21, but when I try to build a test script I'm given errors about how the the "SHT31" var in not defined. (See below)

root@RPiZero1:/downloads# python test1.py
Traceback (most recent call last):
File "test1.py", line 7, in
with SHT31(1) as sht31:
NameError: name 'SHT31' is not defined
<

here is the code that I'm running in test1.py
import fcntl
import struct
import time
import sht31

while True:
with SHT31(1) as sht31:
print sht31.check_heater_status()
sht31.turn_heater_on()
print sht31.check_heater_status()
sht31.turn_heater_off()
print sht31.check_heater_status()
temperature, humidity = sht31.get_temp_and_humidity()
print "Temperature: %s" % temperature
print "Humidity: %s" % humidity

I know the import fcntl, struct, time is not necessary since those are import within the module, but for good measure, I did any what.

Any ideas on how to turn sht#1 into a module correctly.

Thanks

@jaques
Copy link
Owner

jaques commented Feb 20, 2016

Hello,

I'm afraid that this looks like my fault. My example didn't work (I don't have a SHT31 sensor, so I'm guilty of posting code I haven't tested!). I've now updated my example on the README and hopefully it works better. The issue with both of our examples is it should read with sht31.SHT31(1) as sht31: which means use the class SHT31 from the module sht31. We could have also fixed it using from sht31 import SHT31 instead of import sht31 which means that we could then use the class SHT31 directly without needing to qualify it with sht31.SHT31.

I'd recommend reading https://docs.python.org/2/tutorial/modules.html# which explains how python modules work.

Also I wouldn't recommend copying sht31.py into /usr/lib/python2.7/ it's usually best to leave those as they are, just having sht31.py in the current directory should work for importing...

From the doco: "When a module named spam is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named spam.py in a list of directories.." One of the directories it looks in first the current directory (the one with the script in it), so just putting it there should work.

Try removing the other imports as well, as they are imported by sht31.py they shouldn't be needed in your code.

Hope that helps,
Richard.

@happy-jo
Copy link
Author

Richard, thanks so much for the info. It's very helpful.

I did find the sht31.SHT31(1) as sht31 in one of the other issues submitted so I was able to work through it.

I had read about placing the .py in the library folder but in the info about putting it in the same directory helped.

Your script does work and well.

I would say this issue is resolved.

My new issue is I have an I2c bus comm issue when running the module within another.
I'll let you know how it turns out.

Thanks again.

Respectfully,

Joseph Stahl
Mobile

On Feb 20, 2016, at 4:35 AM, jaques [email protected] wrote:

Hello,

I'm afraid that this looks like my fault. My example didn't work (I don't have a SHT31 sensor, so I'm guilty of posting code I haven't tested!). I've now updated my example on the README and hopefully it works better. The issue with both of our examples is it should read with sht31.SHT31(1) as sht31: which means use the class SHT31 from the module sht31. We could have also fixed it using from sht31 import SHT31 instead of import sht31 which means that we could then use the class SHT31 directly without needing to qualify it with sht31.SHT31.

I'd recommend reading https://docs.python.org/2/tutorial/modules.html# which explains how python modules work.

Also I wouldn't recommend copying sht31.py into /usr/lib/python2.7/ it's usually best to leave those as they are, just having sht31.py in the current directory should work for importing...

From the doco: "When a module named spam is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named spam.py in a list of directories.." One of the directories it looks in first the current directory (the one with the script in it), so just putting it there should work.

Try removing the other imports as well, as they are imported by sht31.py they shouldn't be needed in your code.

Hope that helps,
Richard.


Reply to this email directly or view it on GitHub.

@happy-jo
Copy link
Author

So now I'm trying to create a loop with your code. everything so far has worked but I'm having an issue with the "while True:"

so your code:
import sht31
with sht31.SHT31(1) as sht31:
print sht31.check_heater_status()
sht31.turn_heater_on()
print sht31.check_heater_status()
sht31.turn_heater_off()
print sht31.check_heater_status()
temperature, humidity = sht31.get_temp_and_humidity()
print "Temperature: %s" % temperature
print "Humidity: %s" % humidity

Works great, but I want to take it one step further by looping the data and adding a sleep to it. Everything work until I add the while True: to it like so.

import sht31
while True:
with sht31.SHT31(1) as sht31:
print sht31.check_heater_status()
sht31.turn_heater_on()
print sht31.check_heater_status()
sht31.turn_heater_off()
print sht31.check_heater_status()
temperature, humidity = sht31.get_temp_and_humidity()
print "Temperature: %s" % temperature
print "Humidity: %s" % humidity
time.sleep(10)

the function will grab the data for the first run but then gives me the following

root@RPiZero1:/downloads# python test.py
False
True
False
Temperature: 23.1938171387
Humidity: 31.3110351562
Traceback (most recent call last):
File "test.py", line 3, in <module>
```with sht31.SHT31(1) as sht31: AttributeError: SHT31 instance has no attribute 'SHT31'`

Any ideas.

link to formatted code https://github.com/happy-jo/works_in_progress/blob/master/test.py

@jaques
Copy link
Owner

jaques commented Feb 28, 2016

Hello, Sorry about the slow response.

This looks to me like it's an indentation issue, unlike many other programming languages, indentation does matter in python (see https://docs.python.org/2/reference/lexical_analysis.html#indentation for more info on indentation in python).

The example uses the with syntax which will close/cleanup things at the end of the with block (https://www.python.org/dev/peps/pep-0343/ explains more. The sht31 object may have been closed in your example (closing the underlying i2c connections).

Perhaps try updating the indentation of your code like this:

import sht31
while True:
    with sht31.SHT31(1) as sht31:
        print sht31.check_heater_status()
        sht31.turn_heater_on()
        print sht31.check_heater_status()
        sht31.turn_heater_off()
        print sht31.check_heater_status()
        temperature, humidity = sht31.get_temp_and_humidity()
        print "Temperature: %s" % temperature
        print "Humidity: %s" % humidity
        time.sleep(10)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants