You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
And use the library in one of two ways: by running your script through it or by importing a context manager from there.
43
+
And use the library in one of three ways: by typing commands via REPL, by running your script through it or by importing a context manager from there.
43
44
44
45
If you run the script [like this](#script-launch-mode), all dependencies will be automatically installed when the application starts and deleted when it stops:
45
46
46
47
```bash
47
48
instld script.py
48
49
```
49
50
51
+
The [REPL mode](#repl-mode) works in a similar way, you just need to type `instld` in the console to enter it.
52
+
50
53
You can also call the [context manager](#context-manager-mode) from your code:
51
54
52
55
```python
@@ -59,57 +62,39 @@ with instld('some_package'):
59
62
Read more about each method, its capabilities and limitations below.
60
63
61
64
62
-
## Script launch mode
65
+
## REPL mode
63
66
64
-
You can use `instld` to run your script. To do this, you need to run a command like this in the console:
67
+
REPL mode is the fastest and easiest way to try out other people's libraries for your code. Just type this in your console:
65
68
66
69
```bash
67
-
instld script.py
70
+
instld
68
71
```
69
72
70
-
The contents of the script will be executed in the same way as if you were running it through the `python script.py` command. If necessary, you can pass additional arguments to the command line, as if you are running a regular Python script. However, if your program has imports of any packages other than the built-in ones, they will be installed automatically. Installed packages are automatically cleaned up when you exit the program, so they don't leave any garbage behind.
71
-
72
-
73
-
### Special comment language
74
-
75
-
When using script launch mode, you can specify additional parameters for each import inside your program. To do this, you need to write immediately after it (but always in the same line!) a comment that starts with "instld:", separating key and value pairs with commas.
76
-
77
-
As example, if the name of the imported module and the package name are different, this code imports the `f` function from the [`fazy`](https://github.com/pomponchik/fazy) library version `0.0.3`:
78
-
79
-
```python
80
-
import f # instld: version 0.0.3, package fazy
73
+
After that you will see a welcome message similar to this:
81
74
82
-
print(f('some string'))
83
75
```
76
+
⚡ INSTLD REPL based on
77
+
Python 3.11.6 (main, Oct 2 2023, 13:45:54) [Clang 15.0.0 (clang-1500.0.40.1)] on darwin
78
+
Type "help", "copyright", "credits" or "license" for more information.
84
79
85
-
You can also specify only the version or only the package name in the comment, they do not have to be specified together.
80
+
>>>
81
+
```
86
82
83
+
Enjoy the regular Python [interactive console mode](https://docs.python.org/3/tutorial/interpreter.html#interactive-mode)! Any libraries that you ask for will be installed within the session, and after exiting it, they will be deleted without a trace. You don't need to "clean up" anything after exiting the console.
87
84
88
-
### Using multiple environments
85
+
In this mode, a [special comment language](#special-comment-language) is fully supported.
89
86
90
-
The instld script launch mode provides a unique opportunity to use multiple virtual environments at the same time.
87
+
## Script launch mode
91
88
92
-
Firstly, you can run scripts in the main virtual environment, and it will work exactly as you expect:
89
+
You can use `instld` to run your script from a file. To do this, you need to run a command like this in the console:
93
90
94
91
```bash
95
-
python3 -m venv venv
96
-
source venv/bin/activate
97
92
instld script.py
98
93
```
99
94
100
-
When the "import" command is executed in your script, the package will first be searched in the activated virtual environment, and only then downloaded if it is not found there. Note that by default, the activated virtual environment is read-only. That is, it is assumed that you will install all the necessary libraries there before running your script. If you want to install packages in runtime in a specific virtual environment - read about the second method further.
101
-
102
-
Secondly, you can specify the path to the virtual environment directly [in the comments](#special-comment-language) to a specific import using the `where` directive:
103
-
104
-
```python
105
-
import something # instld: where path/to/the/venv
106
-
```
107
-
108
-
If the path you specified does not exist when you first run the script, it will be automatically created. Libraries installed in this way are not deleted when the script is stopped, therefore, starting from the second launch, the download is no longer required.
95
+
The contents of the script will be executed in the same way as if you were running it through the `python script.py` command. If necessary, you can pass additional arguments to the command line, as if you are running a regular Python script. However, if your program has imports of any packages other than the built-in ones, they will be installed automatically. Installed packages are automatically cleaned up when you exit the program, so they don't leave any garbage behind.
109
96
110
-
Note that the path to the virtual environment in this case should not contain spaces. In addition, there is no multiplatform way to specify directory paths using a comment. Therefore, it is not recommended to use paths consisting of more than one part.
111
-
112
-
Since script launch mode uses a context manager to install packages "under the hood", you should also read about the features of installing packages in this way in the [corresponding section](#using-an-existing-virtual-environment).
97
+
In this mode, as in [REPL](#repl-mode), a [special comment language](#special-comment-language) is fully supported.
113
98
114
99
115
100
## Context manager mode
@@ -174,7 +159,7 @@ with instld('flask==2.0.2') as context_1:
174
159
> ⚠️ Keep in mind that although inter-thread isolation is used inside the library, working with contexts is not completely thread-safe. You can write code in such a way that two different contexts import different modules in separate threads at the same time. In this case, you may get paradoxical results. Therefore, it is recommended to additionally isolate with mutexes all cases where you import something from contexts in different threads.
175
160
176
161
177
-
###Options
162
+
## Options
178
163
179
164
You can use [any options](https://pip.pypa.io/en/stable/cli/pip_install/) available for `pip`. To do this, you need to slightly change the name of the option, replacing the hyphens with underscores, and pass it as an argument to `instld`. Here is an example of how using the `--index-url` option will look like:
180
165
@@ -284,6 +269,48 @@ with instld('flask', catch_output=True):
284
269
The `INFO`[level](https://docs.python.org/3/library/logging.html#logging-levels) is used by default. For errors - `ERROR`.
285
270
286
271
272
+
## Special comment language
273
+
274
+
When using script launch or REPL mode, you can specify additional parameters for each import inside your program. To do this, you need to write immediately after it (but always in the same line!) a comment that starts with "instld:", separating key and value pairs with commas.
275
+
276
+
As example, if the name of the imported module and the package name are different, this code imports the `f` function from the [`fazy`](https://github.com/pomponchik/fazy) library version `0.0.3`:
277
+
278
+
```python
279
+
import f # instld: version 0.0.3, package fazy
280
+
281
+
print(f('some string'))
282
+
```
283
+
284
+
You can also specify only the version or only the package name in the comment, they do not have to be specified together.
285
+
286
+
287
+
## Using multiple environments
288
+
289
+
The instld script launch mode and REPL mode provides a unique opportunity to use multiple virtual environments at the same time.
290
+
291
+
Firstly, you can run scripts in the main virtual environment, and it will work exactly as you expect:
292
+
293
+
```bash
294
+
python3 -m venv venv
295
+
source venv/bin/activate
296
+
instld script.py
297
+
```
298
+
299
+
When the "import" command is executed in your script, the package will first be searched in the activated virtual environment, and only then downloaded if it is not found there. Note that by default, the activated virtual environment is read-only. That is, it is assumed that you will install all the necessary libraries there before running your script. If you want to install packages in runtime in a specific virtual environment - read about the second method further.
300
+
301
+
Secondly, you can specify the path to the virtual environment directly [in the comments](#special-comment-language) to a specific import using the `where` directive:
302
+
303
+
```python
304
+
import something # instld: where path/to/the/venv
305
+
```
306
+
307
+
If the path you specified does not exist when you first run the script, it will be automatically created. Libraries installed in this way are not deleted when the script is stopped, therefore, starting from the second launch, the download is no longer required.
308
+
309
+
Note that the path to the virtual environment in this case should not contain spaces. In addition, there is no multiplatform way to specify directory paths using a comment. Therefore, it is not recommended to use paths consisting of more than one part.
310
+
311
+
Since script launch mode uses a context manager to install packages "under the hood", you should also read about the features of installing packages in this way in the [corresponding section](#using-an-existing-virtual-environment).
312
+
313
+
287
314
## How does it work?
288
315
289
316
This package is essentially a wrapper for `venv` and `pip`.
0 commit comments