Modifications

Sauter à la navigation Sauter à la recherche
3 789 octets ajoutés ,  10 novembre 2016 à 20:12
Ligne 69 : Ligne 69 :  
Now create a Python package on your computer by creating a directory called '''test'''.  Inside that directory create a file called '''add.py''' and place inside it the add function code:
 
Now create a Python package on your computer by creating a directory called '''test'''.  Inside that directory create a file called '''add.py''' and place inside it the add function code:
   −
<syntaxhighlight lang="python">
+
<syntaxhighlight lang="python">
 
def add(a, b):
 
def add(a, b):
 
     return a + b
 
     return a + b
</syntaxhighlight>
+
</syntaxhighlight>
    
Create a file '''subtract.py''' in the same location and place in it the subtract function code:
 
Create a file '''subtract.py''' in the same location and place in it the subtract function code:
   −
<syntaxhighlight lang="python">
+
<syntaxhighlight lang="python">
 
def subtract(a, b):
 
def subtract(a, b):
 
     return a - b
 
     return a - b
</syntaxhighlight>
+
</syntaxhighlight>
 +
 +
Finally create a file called '''__init__.py''' in the same location and enter the following code to import and expose the functions from the files above:
 +
 
 +
<syntaxhighlight lang="python">
 +
from test.add import add
 +
from test.subtract import subtract
 +
</syntaxhighlight>
 +
 
 +
'''Be sure to call this file exactly __init__.py'''!  Python looks for this file name to know that it found a package.  If you don't have this file or if it's not named correctly then Python will fail to import the package!
 +
 
 +
The code inside '''__init__.py''' runs when the package is imported in a script.  You can see this code imports the add function from the '''add.py''' script in the package, and the subtract function from the '''subtract.py''' script.
 +
 
 +
Notice the import statements refer to '''add.py''' by its full 'absolute import' name of '''test.add'''.  The test. in front of add is the package name, i.e. the name of the package directory.  You can't run a command like "'''from add import add'''" in the __init__.py since Python would get confused if it should load an add.py from inside the module or from elsewhere.  Using the full name '''test.add''' tells Python to use the '''add.py''' module inside the '''test''' package directory.
 +
 
 +
Double check you have a test package structure that looks like the following:
 +
 
 +
* '''test''' directory
 +
** '''__init__.py''' file which imports the add and subtract functions
 +
** '''add.py''' which exposes the add function
 +
** '''subtract.py''' which exposes the subtract function
 +
 
 +
Now try importing and using the package with desktop Python.  In a terminal navigate to the '''parent of the test package directory''' (i.e. one folder above it).  Importing and using a package is just like importing a .py file however Python treats the entire test directory as the package itself.  This means you need to run Python from above the test directory so Python can find the package.
 +
 
 +
'''Also note be sure you don't have a test.py file in the same directory as the test package'''!  If you do Python could get confused and import the test.py file instead of the test package.  Delete test.py if it exists next to the test package folder!
 +
 
 +
Run the following code in the Python REPL to import and use the add and subtract functions from the test package:
 +
 
 +
<syntaxhighlight lang="python">
 +
import test
 +
test.add(1, 1)
 +
test.subtract(1, 1)
 +
</syntaxhighlight>
 +
 
 +
{{ADFImage|FEATHER-CHARGER-MODULE-IMPORT-20.png}}
 +
 
 +
You should see the package imported and the functions work exactly as you saw before!  If you see an ImportError that the test module doesn't exist be sure you're running Python from the '''parent''' of the test package directory!
 +
 
 +
Now try copying the package to your MicroPython board and using it exactly as you did with desktop Python. '''Again make sure a test.py file doesn't exist on the board or else MicroPython will be confused about what to import'''!  If you have test.py on your board already you can delete it using the following ampy command:
 +
 
 +
<nowiki>ampy --port /board/serial/port rm test.py</nowiki>
 +
 
 +
Now create the test package directory in the root of the board:
 +
 
 +
<nowiki>ampy --port /board/serial/port mkdir test</nowiki>
 +
 
 +
And copy inside the three .py files above that define the package (run these commands from '''inside''' the test package folder):
 +
 
 +
<nowiki>ampy --port /board/serial/port put __init__.py test/__init__.py
 +
ampy --port /board/serial/port put add.py test/add.py
 +
ampy --port /board/serial/port put subtract.py test/subtract.py</nowiki>
 +
 
 +
These commands will copy the .py files to the test folder on the board's filesystem.  Use the {{fname|ls}} command to double check there's a test folder with these three files on your board:
 +
 
 +
<nowiki>ampy --port /board/serial/port ls test</nowiki>
 +
 
 +
{{ADFImage|FEATHER-CHARGER-MODULE-IMPORT-21.png}}
 +
 
   −
{{traduction}}
   
{{FEATHER-CHARGER-MODULE-TRAILER}}
 
{{FEATHER-CHARGER-MODULE-TRAILER}}
29 918

modifications

Menu de navigation