1

I'm using Poetry for Python dependency management, along with PyCrate for ASN.1 encoding/decoding.

PyCrate is a dependency pulled from GitHub, and once pulled from GitHub, is installed by running a setup file within the PyCrate directory.

python setup.py install

I would like to integrate the installation step into my pyproject.toml, if possible. My current pyproject.toml includes PyCrate as follows:

…
[tool.poetry.dependencies]
pycrate = {git = "https://github.com/P1sec/pycrate.git"}
…

This will pull PyCrate from the GitHub repository, but into the src folder in the virtualenv created by Poetry.

Is there any way to automatically run the setup script when performing a poetry install? I have looked into using Poetry scripts, but haven't been able to get this up and running correctly as of yet.

My current setup involves running a poetry install, then manually running the setup.py install for PyCrate, however I would like to have my poetry install to perform the full setup, if it can be done.

Any help with this would be much appreciated.

rmc93
  • 13
  • 3

1 Answers1

2

Poetry should already run python setup.py install for you when you run poetry install.

Poetry basically just runs pip install package, which downloads the package, and essentially just runs python setup.py install on the package!

Under the hood, [pip] will run python setup.py install

Source: https://stackoverflow.com/a/15732821/10149169

However, poetry only installs the package inside an isolated virtual environment, to avoid polluting the rest of your computer.

To run something with poetry, you need to run it with poetry run YOUR_COMMAND

In order to run a script inside the virtual environment, you must run poetry shell to enter the virtual environment, or poetry run YOUR_COMMAND. E.g. to run a Python script, you should do poetry run python your_python_script.py

Example

If you have a folder with the following pyproject.toml file:

[tool.poetry]
name = "test"
version = "0.1.0"
description = ""
authors = ["Your Name <you@example.com>"]

[tool.poetry.dependencies]
python = "^3.6"
pycrate = {git = "https://github.com/P1sec/pycrate.git"}

[tool.poetry.dev-dependencies]

[build-system]
requires = ["poetry>=0.12"]
build-backend = "poetry.masonry.api"

After running poetry install, you can access all the pyrcrate scripts by running poetry run SCRIPT_NAME:

# works because pycrate_showmedia.py was installed with poetry install
me@computer:~/example-project$ poetry run poetry run pycrate_showmedia.py
usage: pycrate_showmedia.py [-h] [-bl BL] [-wt] input
pycrate_showmedia.py: error: the following arguments are required: input

If you have a Python file that imports a pycrate library, it also needs to be run using poetry run:

me@computer:~/example-project$ cat test.py 
import pycrate_core
print(pycrate_core.__version__)
me@computer:~/example-project$ poetry run python test.py
Alois Klink
  • 646
  • 8
  • 9
  • That's perfect, thank you for such a detailed explanation! – rmc93 Dec 05 '19 at 10:37
  • I'm confused, this does **not** appear to be automated. If I send this package to someone else, they would also have to run `poetry run post-install.py` – Isaiah Shiner Jan 17 '20 at 17:03
  • Hi @IsaiahShiner, the author was asking about automatically installing dependencies using poetry, which assumes all the dependencies can be installed via `pip`, e.g. by running `python setup.py install`. Python packages usually don't have a `post-install.py` script, since anything in it can just be added to the end of the `setup.py` file. – Alois Klink Jan 17 '20 at 17:48
  • Ah, now I understand what the author was asking. I suppose my question is different: how do I add to setup.py with poetry, since there is no setup.py. I can't find any information on this. – Isaiah Shiner Jan 18 '20 at 15:55
  • @AloisKlink I created my own question: https://stackoverflow.com/questions/59802468/post-install-script-with-python-poetry – Isaiah Shiner Jan 18 '20 at 16:26