Différences entre versions de « FEATHER-CHARGER-MODULE-IMPORT »

De MCHobby - Wiki
Sauter à la navigation Sauter à la recherche
Ligne 3 : Ligne 3 :
 
{{traduction}}
 
{{traduction}}
 
== Importer un fichier de code ==
 
== Importer un fichier de code ==
 +
{{ADFImage|FEATHER-CHARGER-MODULE-IMPORT.png}}
 +
 
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.
 
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.
  

Version du 10 novembre 2016 à 12:18


MCHobby investit du temps et de l'argent dans la réalisation de traduction et/ou documentation. C'est un travail long et fastidieux réalisé dans l'esprit Open-Source... donc gratuit et librement accessible.
SI vous aimez nos traductions et documentations ALORS aidez nous à en produire plus en achetant vos produits chez MCHobby.
Page-under-construction.pngPage(s) en cours de traduction/élaboration.

Page(s) under translation/construction

Importer un fichier de code

{{{2}}}
Crédit: AdaFruit Industries www.adafruit.com

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 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:

def add(a, b):
    return a + b

def subtract(a, b):
    return a - b

Try using the code on your computer first with the 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:

import test
test.add(1, 1)

You should see the add function called and the result of 1 + 1 returned. If you see an 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!

{{{2}}}
Crédit: AdaFruit Industries www.adafruit.com

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:

ampy --port /board/serial/port put test.py

Then connect to the board's REPL and run the same Python code to import and use the module:


import test
test.add(1, 1)
test.subtract(1, 1)

{{{2}}}
Crédit: AdaFruit Industries www.adafruit.com

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:

import test
dir(test)

{{{2}}}
Crédit: AdaFruit Industries www.adafruit.com

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)

Page-under-construction.pngPage(s) en cours de traduction/élaboration.

Page(s) under translation/construction

Source: MicroPython Basics: Loading Modules
Créé par Tony DiCola pour AdaFruit Industries.

Traduit par Meurisse D. pour MCHobby

Toute référence, mention ou extrait de cette traduction doit être explicitement accompagné du texte suivant : «  Traduction par MCHobby (www.MCHobby.be) - Vente de kit et composants » avec un lien vers la source (donc cette page) et ce quelque soit le média utilisé.

L'utilisation commercial de la traduction (texte) et/ou réalisation, même partielle, pourrait être soumis à redevance. Dans tous les cas de figures, vous devez également obtenir l'accord du(des) détenteur initial des droits. Celui de MC Hobby s'arrêtant au travail de traduction proprement dit.

Traduit avec l'autorisation d'AdaFruit Industries - Translated with the permission from Adafruit Industries - www.adafruit.com