Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to force virtualenv creation by default #1726

Closed
noirbizarre opened this issue Mar 1, 2024 · 15 comments
Closed

Allow to force virtualenv creation by default #1726

noirbizarre opened this issue Mar 1, 2024 · 15 comments

Comments

@noirbizarre
Copy link
Contributor

To enable virtualenv creation, we need to add this to the .mise.toml:

[env]
_.python.venv = { path = ".venv", create = true }

This form prevent from applying it globally and doing it with a single command line mise use python@<version>.

It would be nice to have a way to say "I always want a virtualenv created in .venv" so when I run mise use python@<version> it automatically creates the .venv whatever the directory I am into.

(If there is already a way of doing this, I might have missed it, links are welcome)

@honzajavorek
Copy link

honzajavorek commented Mar 5, 2024

I tried to put _.python.venv = { path = ".venv", create = true } to ~/.config/mise/config.toml, but it creates a single .venv in ~/.config/mise/.venv. Which is exactly what is written in the docs (the path is relative to that particular config file), but I had to try it nevertheless, just in case 😀

Having global config for env vars works correctly. E.g. PYTHONDONTWRITEBYTECODE = "1" in ~/.config/mise/config.toml seems to get applied globally.

@honzajavorek
Copy link

In #1438 (comment) I noticed existence of https://github.com/mise-plugins/mise-poetry, which solves my problem.

@noirbizarre
Copy link
Contributor Author

Yes, I tried before submitting this issue 😉

For the mise-plugins/mise-poetry, I think this is not specific to poetry but to any Python project. Most of the time, impact is bigger for non poetry and non pdm projects as they both can create the virtualenv for you. My main case for this is contributing to projects just using setuptools for which I would love to be able to just run mise use python@<version> and be ready to contribute with a proper and clean virtualenv.

@laluka
Copy link

laluka commented Aug 27, 2024

I've been using this short alias for a while to quickly select a python version and setup a venv :
pv='rm .venv ; version="$(mise list python | grep -F python | awk "{ print \$2 }" | fzf)" ; echo -e "[tools]\npython = \"$version\"\n[env]\n_.python.venv = { path = \".venv\", create = true }" | tee .mise.toml'

But it has the huge disadvantage of destroying the current .mise.toml file, which isn't much fun.
That's why I came here looking for a way (i.e. a mise command) to automatically ask for a venv to be created, maybe something like mise use python@3.12.4 --with-venv, or even further, force the .venv to be added to the local mise file by default, and allow preventing this behavior by using --no-venv ?

I'll rework my alias until it's discussed, something to append the right lined if not already present, and rely on mise for inserting the python version in the conf file.. 🙃

@jdx
Copy link
Owner

jdx commented Aug 27, 2024

We could have a setting for this, seems there is plenty of appetite for it.

That said, if it were me I'd use a utility like this: https://github.com/gnprice/toml-cli

We do have mise set which can be used to set env vars, however there isn't a way to use that with directives, maybe we should have something like mise set --directive python.venv --path .venv --create true

@laluka
Copy link

laluka commented Aug 28, 2024

2 years updatelesss.. 😱
But yeah, relying on a toml manipulation is better than my new shitty alias 🤣

#!/bin/bash
set -uo pipefail
IFS=$'\n\t'

version="$(mise list python | grep -F python | awk "{ print \$2 }" | fzf)"
rm -rf .venv
mise use python@${version}
grep -F '[env]' .mise.toml || echo -e '[env]' >> .mise.toml
grep -F '_.python.venv = { path = ".venv", create = true }' .mise.toml || echo '_.python.venv = { path = ".venv", create = true }' >> .mise.toml
mise trust 2>/dev/null
bat -P .mise.toml

@jdx
Copy link
Owner

jdx commented Aug 28, 2024

2 years updatelesss.. 😱

Get outta here with your fake news 😜

@laluka
Copy link

laluka commented Aug 28, 2024

Quel est le fuck ? 🤔 😆 💟

image

@jdx
Copy link
Owner

jdx commented Aug 28, 2024

oh I thought you were talking about this ticket, I was confused since mise hasn't been around 2 years 😂

in any case, weren't we just having a conversation where I said I often forget about what is actually in mise?

if settings.python_venv_auto_create {
info!("setting up virtualenv at: {}", virtualenv.display());

seems I added this setting already but forgot to add it to the docs

@pascal-sun
Copy link

pascal-sun commented Jan 6, 2025

I know the issue is closed, but I have this "simple" alias that works pretty well (and does not destroy the existing mise.toml file):

mise use python@<version> && mise config set env._.python.venv.path .venv && mise config set env._.python.venv.create true

And if you need a brand new virtualenv, such as in @laluka alias, just prepend rm -rf .venv like him:

rm -rf .venv && mise use python@<version> && mise config set env._.python.venv.path .venv && mise config set env._.python.venv.create true

@laluka
Copy link

laluka commented Jan 6, 2025

Thanks pal, I'll replace my script by your alias in a few, makes way more sense to not modify the file by hand, it was a dirty workaround 😅

@hoesler
Copy link

hoesler commented Jan 9, 2025

Instead of an alias, another option is to create a preset task like ~/.config/mise/tasks/preset/python_venv

#!/usr/bin/env bash
#MISE dir="{{cwd}}"
#USAGE arg "<version>"

mise use python@$usage_version
mise config set env._.python.venv.path .venv
mise config set --type bool env._.python.venv.create true
mise config set --type list env._.python.venv.uv_create_args -- --seed

With this, you can simply create a venv by running mise preset:python_venv 3.11.

@laluka
Copy link

laluka commented Jan 9, 2025

image

@laluka
Copy link

laluka commented Feb 8, 2025

Hmhm, any clue why this doesn't seem to work anymore? 🥺 cc @hoesler @jdx

Image

@laluka
Copy link

laluka commented Feb 8, 2025

Nvm I'm a moron, missing chmod +x, I assumed it was sourced, not exec'd, sorry for the ping 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants