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.
Le type Strath
La bibliothèque strath, une dépendance de syspathmodif, convertit les
chemins de fichier en chaînes de caractères (str) ou en instances de
pathlib.Path. Elle définit l'alias de type (TypeAlias) Strath comme un
équivalent de str | Path.
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_appendajoute le chemin donné à la fin desys.path.sp_containsindique sisys.pathcontient le chemin donné.sp_prependajoute le chemin donné au début desys.path.sp_removeenlève le chemin donné desys.path.
Au moment de sa création, une instance de SysPathBundle contient plusieurs
chemins et les ajoute au début de 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. Elle
indique si le dictionnaire sys.modules contient ce module.
Les fonctions suivantes permettent d'ajouter au début de sys.path le chemin
de dossiers parents du fichier qui les appelle. Des indices passés en argument
identifient les parents. Soit une instance p de pathlib.Path représentant
le chemin du fichier appelant. Le dossier parent identifié par l'indice i
passé à ces fonctions correspond au chemin renvoyé par p.parents[i].
sp_prepend_parentajoute un chemin parent àsys.path.sp_prepend_parent_bundlepasse un ou plusieurs chemins parents à une instance deSysPathBundlepuis renvoie cette dernière.
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 si la liste sys.path contient le chemin
de son dossier parent. On peut donc rendre un module importable en ajoutant son
chemin parent à sys.path.
Importations et sys.modules
Le dictionnaire sys.modules associe des noms (str) de module au module
correspondant. Le système d'importation l'utilise comme cache; tout module
importé pour la première fois y est conservé. Puisque le système d'importation
cherche d'abord les modules demandés dans sys.modules, les modules qu'il
contient 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 est déjà importable. Si sm_contains renvoie vrai (True), il n'est
pas nécessaire de modifier sys.path pour importer le module donné.
ENGLISH
This library offers concise manners to modify list sys.path.
The user should not need to directly interact with that list.
Type Strath
Library strath, a dependency of syspathmodif, converts file paths to
character strings (str) or pathlib.Path instances. It defines type alias
(TypeAlias) Strath as an equivalent of str | Path.
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_appendadds the given path to the end ofsys.path.sp_containsindicates whethersys.pathcontains the given path.sp_prependadds the given path to the beginning ofsys.path.sp_removeremoves the given path fromsys.path.
Upon creation, a SysPathBundle instance stores several paths and prepends
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 name (str) as an argument. It
indicates whether dictionary sys.modules contains the module.
The following functions allow to prepend to sys.path the paths to parent
directories of the file that calls them. Indices passed as arguments identify
the parents. Let be a pathlib.Path instance p representing the path to the
calling file. The parent directory identified by index i passed to these
functions matches the path returned by p.parents[i].
sp_prepend_parentprepends one parent path tosys.path.sp_prepend_parent_bundlepasses one or many parent paths to aSysPathBundlethen returns the bundle.
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 if list sys.path contains the path to its
parent directory. Therefore, you can make a module importable by adding its
parent path to sys.path.
Imports and sys.modules
Dictionary sys.modules maps module names (str) to the corresponding module.
The import system uses it as a cache; any module imported for the first time is
stored in it. Since the import system looks for the requested modules in
sys.modules first, the modules that it contains can be imported everywhere
with no modifications to sys.path.
Knowing this, you can use function sm_contains to determine if a module is
already importable. If sm_contains returns True, modifiying sys.path is
not required to import the given module.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file syspathmodif-1.7.0.tar.gz.
File metadata
- Download URL: syspathmodif-1.7.0.tar.gz
- Upload date:
- Size: 12.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6c5b3a4000f49c835db1e4a75ef1d954128b2bd41f0439d33e58925b960e3b28
|
|
| MD5 |
f4cafe8e15e43ba61a6288f7c78962c0
|
|
| BLAKE2b-256 |
5b3640fdd51ac142bba5a2fbfc1d1572053c3cc15d9ea7129d85b0ca3cf7206a
|