Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to run a script using pyproject.toml settings and poetry?

Tags:

  1. I am using poetry to create .whl files.
  2. I have an ftp sever runing on a remote host.
  3. I wrote a python script (log_revision.py) which save in a database the git commit, few more parameters and in the end send the the .whl(that poetry created) to the remote server ( each .whl in a different path in the server, the path is save in the db) .

At the moment I run the script manually after each time I run the poetry build commend. I know the pyproject.toml has the [tool.poetry.scripts] but i dont get how can i use it to run a python script.

I tried

[tool.poetry.scripts] my-script = "my_package_name:log_revision.py 

and then poetry run my-script but I allways get an error AttributeError: module 'my_package_namen' has no attribute 'log_revision'

1. can some one please help me understand how to run to wish commend?

as a short term option(with out git and params) i tried to use the poetry publish -r http://192.168.1.xxx/home/whl -u hello -p world but i get the following error

[RuntimeError]                                  Repository http://192.168.1.xxx/home/whl is not defined   

2. what am i doing wring and how can i fix it?

would appricate any help, thx!

like image 867
helpper Avatar asked Dec 11 '19 13:12

helpper


People also ask

How do you run a script in a poem?

To run your script simply use poetry run python your_script.py . Likewise if you have command line tools such as pytest or black you can run them using poetry run pytest .

What is Pyproject toml file?

The pyproject. toml file was introduced in PEP-518 (2016) as a way of separating configuration of the build system from a specific, optional library (setuptools) and also enabling setuptools to install itself without already being installed.


2 Answers

At the moment the [tool.poetry.scripts] sections is equivalent to setuptools console_scripts.

So the argument must be a valid module and method name. Let's imagine within your package my_package, you have log_revision.py, which has a method start(). Then you have to write:

[tool.poetry.scripts] my-script = "my_package.log_revision:start" 

Here's a complete example:

You should have this folder structure:

my_package ├── my_package │   ├── __init__.py │   └── log_revision.py └── pyproject.toml 

The content of pyproject.toml is:

[tool.poetry] name = "my_package" version = "0.1.0" description = "" authors = ["Your Name <[email protected]>"]  [tool.poetry.dependencies] python = "^3.8"  [tool.poetry.scripts] my-script = "my_package.log_revision:start"  [build-system] requires = ["poetry_core>=1.0.0"] build-backend = "poetry.core.masonry.api" 

and of log_revision.py:

def start():     print("Hello") 

After you have run poetry install once you should be able to do this:

$ poetry run my-script   Hello 

You cannot pass something to the start() method directly. Instead you can use command line arguments and parse them, e.g. with pythons argparse.

like image 76
finswimmer Avatar answered Oct 18 '22 12:10

finswimmer


Although the previous answers are correct, they are a bit complicated. The simplest way to run a python script with poetry is as follows:

poetry run python myscript.py 

If you are using a dev framework like streamlit you can use

poetry run streamlit run myapp.py 

Basically anything you put after poetry run will execute from the poetry virtual environment.

like image 34
DaveR Avatar answered Oct 18 '22 14:10

DaveR