jobin-sun / raspberry-gpio-python

Automatically exported from code.google.com/p/raspberry-gpio-python
MIT License
0 stars 0 forks source link

RPi.GPIO in a module #49

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago

What steps will reproduce the problem?

1. I try to create a module "myinit_GPI"
   and then import the module -  import mymodule
   In this simple module

   def myinit_GPI():
       GPIO.setmode(GPIO.BCM)
       GPIO.setwarnings(False)

       GPIO.setup(2, GPIO.OUT)
       GPIO.setup(3, GPIO.OUT)

       GPIO.output(2,1)
       GPIO.output(2,0)
       return

2.  From the main prog I like to init ... 

3.  #!/usr/bin/python
    import RPi.GPIO as GPIO
    import mymodule    

    mymodule.myinit_GPI()

4.  I revieved an error

    mymodule.myinit_GPI()
    AttributeError: 'module' object has no attribute 'myinit_GPI'

    QUE !?!?!?

I use PYTHON - v2.7.3 And Raspian 2013-07-26-wheezy-raspbian.zip
I'm a beginner of Raspian and also a Python beginner ...

Best Regards  Peffag 

Original issue reported on code.google.com by pef...@gmail.com on 3 Nov 2013 at 8:34

GoogleCodeExporter commented 9 years ago
Some more information ...

Traceback (most recent call last):
  File "./ut.py", line 9, in <module>
    mymodule.myinit_GPI()
  File "/home/pi/scripts/mymodule.py", line 2, in myinit_GPI
    GPIO.setmode(GPIO.BCM)
NameError: global name 'GPIO' is not defined

Original comment by pef...@gmail.com on 3 Nov 2013 at 8:40

GoogleCodeExporter commented 9 years ago
More Info :  If I put all code in the same main program it work fine !!!
             The problem is only if I use import of mymodule.myinit_GPI()
             If I add som mymodule.simple() it works fine.
             "simple()
             "print "Hi"
             "return 

Original comment by pef...@gmail.com on 3 Nov 2013 at 1:32

GoogleCodeExporter commented 9 years ago
This is not an issue with RPi.GPIO.  It is an issue with your code.

To fix your problem, add the line:
import RPi.GPIO as GPIO
at the beginning of mymodule.py

You need to import every dependency of a module at the top of a module, just 
like you would in your main program.

Original comment by btcros...@googlemail.com on 3 Nov 2013 at 8:12