-
Notifications
You must be signed in to change notification settings - Fork 19
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
Comments
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 I'd recommend reading https://docs.python.org/2/tutorial/modules.html# which explains how python modules work. Also I wouldn't recommend copying From the doco: "When a module named Try removing the other imports as well, as they are imported by Hope that helps, |
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. Thanks again. Respectfully, Joseph Stahl
|
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: 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.
the function will grab the data for the first run but then gives me the following
Any ideas. link to formatted code https://github.com/happy-jo/works_in_progress/blob/master/test.py |
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 Perhaps try updating the indentation of your code like this:
|
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)
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
The text was updated successfully, but these errors were encountered: