0 added
0 removed
Original
2026-01-01
Modified
2026-02-21
1
<p>When we develop a program for the end user, we put<strong>entry points</strong>in the code and get modules or packages ready to run.</p>
1
<p>When we develop a program for the end user, we put<strong>entry points</strong>in the code and get modules or packages ready to run.</p>
2
<p>But even if you do everything correctly, the user still has to write a very long command, python3 -m FULL_NAME_MODULE.</p>
2
<p>But even if you do everything correctly, the user still has to write a very long command, python3 -m FULL_NAME_MODULE.</p>
3
<p>To simplify this, developers design their packages so users can enter short commands. Poetry calls these commands<strong>scripts</strong>.</p>
3
<p>To simplify this, developers design their packages so users can enter short commands. Poetry calls these commands<strong>scripts</strong>.</p>
4
<h2>Entry point in the example project</h2>
4
<h2>Entry point in the example project</h2>
5
<p>The entry point to the program can be any function located in any module. Let's place the entry point in the hello.scripts.say_hello module to keep things in order:</p>
5
<p>The entry point to the program can be any function located in any module. Let's place the entry point in the hello.scripts.say_hello module to keep things in order:</p>
6
<p>Check that the entry point works when the long command is specified:</p>
6
<p>Check that the entry point works when the long command is specified:</p>
7
<p>Usually, all the complicated logic is placed somewhere in the library code. That's in the hello package in our case. But the scripts only contain the small code fragments necessary to run them.</p>
7
<p>Usually, all the complicated logic is placed somewhere in the library code. That's in the hello package in our case. But the scripts only contain the small code fragments necessary to run them.</p>
8
<p>Suppose you've written some essential code in one of the scripts package modules and want to import it elsewhere. In that case, you should stop and consider where that code should be. In other words, scripts can import from a library but not vice versa.</p>
8
<p>Suppose you've written some essential code in one of the scripts package modules and want to import it elsewhere. In that case, you should stop and consider where that code should be. In other words, scripts can import from a library but not vice versa.</p>
9
<h2>Description of the scripts in the configuration file</h2>
9
<h2>Description of the scripts in the configuration file</h2>
10
<p>Now we know what scripts are, so the next step is to learn how to write them.</p>
10
<p>Now we know what scripts are, so the next step is to learn how to write them.</p>
11
<p>Open pyproject.toml and find the tool.poetry.dependencies section. Immediately after it, add a new section, tool.poetry.scripts, which looks like this:</p>
11
<p>Open pyproject.toml and find the tool.poetry.dependencies section. Immediately after it, add a new section, tool.poetry.scripts, which looks like this:</p>
12
<p>Let us look at how a key-value pair works:</p>
12
<p>Let us look at how a key-value pair works:</p>
13
<ul><li>The key is the name of the future short command</li>
13
<ul><li>The key is the name of the future short command</li>
14
<li>The value is the full module name, colon, and declared function name</li>
14
<li>The value is the full module name, colon, and declared function name</li>
15
</ul><p>Note the difference in the names:</p>
15
</ul><p>Note the difference in the names:</p>
16
<ul><li>The command is called say-hello</li>
16
<ul><li>The command is called say-hello</li>
17
<li>The module is called say_hello</li>
17
<li>The module is called say_hello</li>
18
</ul><p>Compound command names are usually hyphenated, but Python module names can't have this symbol. Therefore, in this case, the hyphen is replaced by an underscore.</p>
18
</ul><p>Compound command names are usually hyphenated, but Python module names can't have this symbol. Therefore, in this case, the hyphen is replaced by an underscore.</p>
19
<p>Remember that hello.scripts.say_hello is the full module name, which looks like the path hello/scripts/say_hello.py at first glance. If you accidentally give the file path instead of the module name or forget to enter the function name, the short command call will end with an error.</p>
19
<p>Remember that hello.scripts.say_hello is the full module name, which looks like the path hello/scripts/say_hello.py at first glance. If you accidentally give the file path instead of the module name or forget to enter the function name, the short command call will end with an error.</p>
20
<p>If we describe everything correctly, Poetry will allow us to call the newly declared short command in the usual way:</p>
20
<p>If we describe everything correctly, Poetry will allow us to call the newly declared short command in the usual way:</p>
21
21