Build python package to wheel and sdist.

10 views
Skip to first unread message

yfprojects

unread,
May 30, 2022, 1:03:15 PMMay 30
to python-doit
Hello,

I would like to use doit for packaging my python project as python package using the `build` module. For each task execution I want to configure the setup of a new virtual environment that is then used to install python packages in it as well as running `python -m build`.
Can anyone help me configuring the virtual environment bit? Thanks!

Best regards
yfprojects

Eduardo Schettino

unread,
May 30, 2022, 1:07:39 PMMay 30
to python-doit
Hi,

The project TOX manages virtual environments out of the box. Maybe that works for you.

Regards

--
You received this message because you are subscribed to the Google Groups "python-doit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-doit...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python-doit/98a8b627-0e20-4d95-b9c4-79ef391ed833n%40googlegroups.com.

yfprojects

unread,
May 31, 2022, 1:51:05 AMMay 31
to python-doit
Thank you for your quick reply. I don't think `tox` suits my needs. I can use the python `virtualenv` module to create a virtual environment using a python API, but I don't know how to run subsequent task in that environment.

Eduardo Schettino

unread,
May 31, 2022, 4:34:15 AMMay 31
to python-doit
That is not related to doit at all, but...

Your virtualenv path will contain a `bin` folder, it will contain a `python` for that env and any other entry_points installed.
i.e.

/home/my-name/.virtualenvs/my-env/bin/python3
/home/my-name/.virtualenvs/my-env/bin/gunicorn

Then you will have to execute everything as scripts ("cmd-action" not as "python-action").
Would that work for you?

Regards




yfprojects

unread,
Jun 5, 2022, 3:18:09 AMJun 5
to pytho...@googlegroups.com
Then you will have to execute everything as scripts ("cmd-action" not as "python-action").

That's what I was already opting for. However simply calling the executables in the virtual environment isn't the same as sourcing it. This is because the activation script will modify some environment variables like `PATH`.

If you directly run a script or the python interpreter from the virtualenv’s bin/ directory (e.g. path/to/ENV/bin/pip or /path/to/ENV/bin/python-script.py) then sys.path will automatically be set to use the Python libraries associated with the virtualenv. But, unlike the activation scripts, the environment variables PATH and VIRTUAL_ENV will not be modified. This means that if your Python script uses e.g. subprocess to run another Python script (e.g. via a !/usr/bin/env python shebang line) the second script may not be executed with the same Python binary as the first nor have the same libraries available to it. To avoid this happening your first script will need to modify the environment variables in the same manner as the activation scripts, before the second script is executed.

To solve that I had a look at the activation scripts. Besides modifying some environment variables they only add a `deactivate` command and modify the prompt. Both is not needed for running commands outside a shell. I replicated the env modification with the following code:

def source_venv(path: Path):
"""Prepare a bash environment."""
environment = os.environ.copy()
environment['VIRTUAL_ENV'] = str(path.resolve())
if sys.platform == 'win32':
script_path = 'Scripts'
else:
script_path = 'bin'
environment['PATH'] = str(
(path / script_path).resolve()) + os.pathsep + environment['PATH']
environment.pop('PYTHON_HOME', None)
return environment
def CmdInVenv(venv: Path, cmd, **kwargs):
"""Run task action in a virtual env"""
kwargs.setdefault('env', {}).update(source_venv(venv))
if isinstance(cmd, list):
kwargs.setdefault('shell', False)
return CmdAction(cmd, **kwargs)



Am 31.05.22 um 07:51 schrieb yfprojects:
You received this message because you are subscribed to a topic in the Google Groups "python-doit" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/python-doit/4Dh2eNtzigg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to python-doit...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python-doit/524ae8a1-1bff-4a47-b042-c4b0c0a02088n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages