Modifications

Sauter à la navigation Sauter à la recherche
421 octets ajoutés ,  11 novembre 2016 à 13:07
Ligne 98 : Ligne 98 :  
from test.subtract import subtract
 
from test.subtract import subtract
 
  </syntaxhighlight>
 
  </syntaxhighlight>
  −
{{traduction}}
      
'''Soyez attentif, le fichier doit exactement s'appeler __init__.py'''!  Python recherche ce nom de fichier pour savoir s'il s'agit d'un paquet/package. Si vous n'avez pas ce fichier ou s'il nest pas nommé correctement alors Python n'importera pas le paquet!
 
'''Soyez attentif, le fichier doit exactement s'appeler __init__.py'''!  Python recherche ce nom de fichier pour savoir s'il s'agit d'un paquet/package. Si vous n'avez pas ce fichier ou s'il nest pas nommé correctement alors Python n'importera pas le paquet!
Ligne 105 : Ligne 103 :  
Le code à l'intérieur du fichier '''__init__.py''' est exécuté lorsque le paquet/package est importé par un script. Vous pouvez voir dans le code de __init__.py qu'il importe la fonction {{fname|add}} depuis le fichier '''add.py''' (contenu dans le paquet/package) ainsi que la fonction {{fname|substract}} contenu dans le fichier '''subtract.py'''.
 
Le code à l'intérieur du fichier '''__init__.py''' est exécuté lorsque le paquet/package est importé par un script. Vous pouvez voir dans le code de __init__.py qu'il importe la fonction {{fname|add}} depuis le fichier '''add.py''' (contenu dans le paquet/package) ainsi que la fonction {{fname|substract}} contenu dans le fichier '''subtract.py'''.
   −
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.
+
Notez que l'instruction d'importation référence le fichier '''add.py''' en utilisant un "''nom d'importation absolu''". Par exemple, le fonction add est importée depuis '''test.add'''.  Le 'test.' est le nom du paquet/package, par exemple: le nom du répertoire du paquet/package. Le 'add' derrière le 'test.add' est le nom du fichier python (add.py) où l'interpréteur Python trouvera la fonction à importer.
 +
 
 +
Suivant l'instruction {{fname|from ''test.add'' import '''add'''}}, nous importons donc une fonction '''add''' depuis le fichier ''add.py'' se trouvant dans le ''répertoire "test"'' (le "''test.add''").  
 +
 
 +
Vous  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:
 
Double check you have a test package structure that looks like the following:
29 918

modifications

Menu de navigation