Skip to main content

This library offers concise manners to modify list sys.path. The user should not need to directly interact with that list.

Project description

FRANÇAIS

Cette bibliothèque offre des manières concises de modifier la liste sys.path. L'utilisateur ne devrait pas avoir besoin d'interagir directement avec cette liste.

Contenu

Les fonctions suivantes prennent un chemin de type str ou pathlib.Path comme argument. Elles convertissent les arguments de type pathlib.Path en str puisque sys.path n'est censée contenir que des chaînes de caractères.

  • sp_append ajoute le chemin donné à la fin de sys.path.
  • sp_contains indique si sys.path contient le chemin donné.
  • sp_remove enlève le chemin donné de sys.path.

Dès son instanciation, la classe SysPathBundle contient plusieurs chemins et les ajoute à sys.path. Quand on vide (clear) une instance, elle efface son contenu et l'enlève de sys.path. Ainsi, cette classe facilite l'ajout et le retrait d'un groupe de chemins.

Il est possible d'utiliser SysPathBundle comme un gestionnaire de contexte (context manager). Dans ce cas, l'instance est vidée à la fin du bloc with.

La fonction sm_contains prend comme argument un nom (str) de module ou de paquet. Elle indique si le dictionnaire sys.modules contient ce module ou paquet.

Pour plus d'informations, consultez la documentation et les démos dans le dépôt de code source.

Importations et sys.path

Il est possible d'importer un module ou un paquet si la liste sys.path contient le chemin de son dossier parent. On peut donc rendre un module ou un paquet importable en ajoutant son chemin parent à sys.path.

Importations et sys.modules

Le dictionnaire sys.modules associe des noms (str) de module ou de paquet au module ou paquet correspondant. Quand un module ou un paquet est importé pour la première fois, il est ajouté à sys.modules. Puisque le système d'importation cherche d'abord les modules et paquets demandés dans sys.modules, il évite de les charger plus d'une fois. De plus, les modules et paquets présents dans sys.modules peuvent être importés partout sans qu'on modifie sys.path.

Sachant cela, on peut déterminer à l'aide de la fonction sm_contains si un module ou un paquet est déjà importable. Si sm_contains renvoie vrai (True), il n'est pas nécessaire de modifier sys.path pour importer le module ou le paquet donné.

ENGLISH

This library offers concise manners to modify list sys.path. The user should not need to directly interact with that list.

Content

The following functions take a path of type str or pathlib.Path as an argument. They convert arguments of type pathlib.Path to str since sys.path is supposed to contain only character strings.

  • sp_append appends the given path to the end of sys.path.
  • sp_contains indicates whether sys.path contains the given path.
  • sp_remove removes the given path from sys.path.

Upon instantiation, class SysPathBundle stores several paths and adds them to sys.path. When a bundle is cleared, it erases its content and removes it from sys.path. Thus, this class facilitates adding and removing a group of paths.

SysPathBundle can be used as a context manager. In that case, the instance is cleared at the with block's end.

Function sm_contains takes a module's or package's name (str) as an argument. It indicates whether dictionary sys.modules contains the module or package.

For more information, consult the documentation and the demos in the source code repository.

Imports and sys.path

It is possible to import a module or package if list sys.path contains the path to its parent directory. Therefore, you can make a module or package importable by adding its parent path to sys.path.

Imports and sys.modules

Dictionary sys.modules maps module and package names (str) to the corresponding module or package. When a module or package is imported for the first time, it is added to sys.modules. Since the import system looks for the requested modules and packages in sys.modules first, it avoids loading them more than once. Moreover, the modules and packages in sys.modules can be imported everywhere with no modifications to sys.path.

Knowing this, you can use function sm_contains to determine if a module or package is already importable. If sm_contains returns True, modifiying sys.path is not required to import the given module or package.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

syspathmodif-1.4.0.tar.gz (9.9 kB view details)

Uploaded Source

File details

Details for the file syspathmodif-1.4.0.tar.gz.

File metadata

  • Download URL: syspathmodif-1.4.0.tar.gz
  • Upload date:
  • Size: 9.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for syspathmodif-1.4.0.tar.gz
Algorithm Hash digest
SHA256 73495b2003d8f8f3a1799108f93220d55ff8d665206589e98014835e13628488
MD5 eead0cf9113584b5ff8774858a0e6310
BLAKE2b-256 6386248efa8832bf24754549dc8a1dc178785b9296fa8bed79337d36575c80a3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page