Modifications

Sauter à la navigation Sauter à la recherche
4 007 octets ajoutés ,  10 novembre 2016 à 12:15
aucun résumé de modification
Ligne 1 : Ligne 1 :  
{{FEATHER-CHARGER-MODULE-NAV}}
 
{{FEATHER-CHARGER-MODULE-NAV}}
    +
{{traduction}}
 +
== Importer un fichier de code ==
 +
Just like with regular Python you can import and use code from files in your own MicroPython scripts.  This is great for breaking a large or complex script into smaller pieces, or for sharing and reusing code with multiple projects.
 +
 +
If you aren't familiar with Python's module support be sure to [https://docs.python.org/3/tutorial/modules.html read the official documentation first].  Python allows you to put code in a .py file and import it from other scripts in the same directory.  You can even get more advanced and create packages which include multiple .py files and expose them in different ways.  Most third-party Python libraries are available as packages which you install and import in your own scripts.
 +
 +
We'll start by looking at how to import code from a single '''.py''' file in your MicroPython script.  First make sure you have a board running MicroPython and are familiar with copying files to and from the board.
 +
 +
Next start by creating a simple Python file with a few functions on your computer.  In a text editor create '''test.py''' and fill it with the following code:
 +
 +
<syntaxhighlight lang="python">
 +
def add(a, b):
 +
    return a + b
 +
 +
def subtract(a, b):
 +
    return a - b
 +
</syntaxhighlight>
 +
 +
Try using the code on your computer first with the [http://www.python.org/ desktop version of Python] before trying it in MicroPython.  In a terminal navigate to the same directory as the test.py file (this is very important, '''you must be in the same directory as test.py'''!) and run the '''python3''' command (or '''python''' if using Python 2.x).  At the Python REPL enter the following commands:
 +
 +
<syntaxhighlight lang="python">
 +
import test
 +
test.add(1, 1)
 +
</syntaxhighlight>
 +
 +
You should see the add function called and the result of 1 + 1 returned.  If you see an {{fname|ImportError}} that the test module can't be found make sure you're running Python from the same directory as test.py is located.
 +
 +
Try calling the subtract function just like the add function was called.  Remember you need to add the module name in front of the function when you call it!
 +
 +
{{ADFImage|FEATHER-CHARGER-MODULE-IMPORT-10.png}}
 +
 +
Now that you see how a simple .py file import works on your computer try doing the same with MicroPython.  Copy the test.py file to the root of your board's filesystem.  For example if you're using a tool like ampy to copy files you would run something like:
 +
 +
<nowiki>ampy --port /board/serial/port put test.py</nowiki>
 +
 +
Then connect to the board's REPL and run the same Python code to import and use the module:
 +
 +
 +
<syntaxhighlight lang="python">
 +
import test
 +
test.add(1, 1)
 +
test.subtract(1, 1)
 +
</syntaxhighlight>
 +
 +
{{ADFImage|FEATHER-CHARGER-MODULE-IMPORT-11.png}}
 +
 +
You should see the functions run just like they did on your computer!  If you see an ImportError double check you copied the test.py file to the root of the board's filesystem and try again.
 +
 +
Importing and using code from a .py file in MicroPython is as easy as copying the file to the board and importing to use as above.  Remember the file needs to be in the same location as the script which is importing and using it.  In most cases your scripts will be in the root of the board's filesystem so that's usually where you want to place .py files which will be imported.
 +
 +
You can import and call more than just functions in your scripts too.  Anything inside test.py like classes, functions, global variables, etc. will be availabe to your script after the import command runs.  In fact you can see exactly what is in the module with the '''dir''' command, for example in the REPL run:
 +
 +
<syntaxhighlight lang="python">
 +
import test
 +
dir(test)
 +
</syntaxhighlight>
 +
 +
{{ADFImage|FEATHER-CHARGER-MODULE-IMPORT-12.png}}
 +
 +
You should see a list of everything that was imported from the module, including the '''add''' and subtract functions (the '''__name__''' variable is something Python adds to let the module know what its name is).
 +
 +
== Les paquets (Packages) ==
 
{{traduction}}
 
{{traduction}}
    
{{FEATHER-CHARGER-MODULE-TRAILER}}
 
{{FEATHER-CHARGER-MODULE-TRAILER}}
29 917

modifications

Menu de navigation