conan icon indicating copy to clipboard operation
conan copied to clipboard

[question] how to use custom python version in pipenv

Open TareHimself opened this issue 3 months ago • 5 comments

What is your question?

Is there a recommended way to use a custom version of Python in Pipenv? I tried using pyenv, but it requires confirmation when installing versions, so I don't think that would work in CI

Have you read the CONTRIBUTING guide?

  • [x] I've read the CONTRIBUTING guide

TareHimself avatar Nov 24 '25 00:11 TareHimself

Hi @TareHimself

The docs in https://docs.conan.io/2/reference/tools/system/pipenv.html#pipenv say that you can use the tools.system.pipenv:python_interpreter conf to define the Python interpreter that you want, is this what you were looking for?

memsharded avatar Nov 25 '25 17:11 memsharded

That helps if the Python version already exists, but I was thinking about installing a different Python in the virtual env, in cases where Python on the system is not supported by the package I want to build. The idea is

Create Env with python=x version => Do stuff i.e. install packages host system python remains unaffected

TareHimself avatar Nov 25 '25 18:11 TareHimself

That helps if the Python version already exists, but I was thinking about installing a different Python in the virtual env, in cases where Python on the system is not supported by the package I want to build. The idea is

Not sure if I follow. To create a venv you need a Python in the system in the first place. You cannot create a venv and then install a different Python inside it.

With the conf I suggested you can create a PipEnv for any custom Python version, but that version has to be first installed in the system, yes, otherwise it is not possible to create the venv in the first place.

memsharded avatar Nov 25 '25 19:11 memsharded

I found uv, which allows me to install Python versions. This is the setup I currently use to install and use custom Python versions

def get_system_python(self):
    python = "python" if platform.system() == "Windows" else "python3"
    default_python = shutil.which(python)
    return os.path.realpath(default_python) if default_python else None

def get_default_python(self):
    return self.conf.get("tools.system.pipenv:python_interpreter") or self.get_system_python()

def add_dir_to_path(self,bin_dir,env_name="conan_env"):
    env = Environment()
    env.prepend_path("PATH", bin_dir)
    env.vars(self).save_script(env_name)

def get_env_bin_dir(self,env_dir):
    return os.path.join(env_dir,"Scripts" if platform.system() == "Windows" else "bin")

def get_env_python(self,env_dir):
    return os.path.join(self.get_env_bin_dir(env_dir), "python.exe" if platform.system() == "Windows" else "python")

# setup an env to install uv into
python = self.get_default_python()

uv_install_env_dir = os.path.join(self.build_folder,'.uv_install_env')

# use default python to make an env
self.run(cmd_args_to_string([python, '-m', 'venv', uv_install_env_dir]))

# install uv in the env we just made
self.run(cmd_args_to_string([self.get_env_python(uv_install_env_dir),'-m','pip', 'install', 'uv']))

uv_env_name = 'uv_env'
uv_env_dir = os.path.join(self.build_folder,f'.{uv_env_name}')

# use the uv env we just made to create another env with the python version we need
self.run(cmd_args_to_string([self.get_env_python(uv_install_env_dir),'-m', 'uv', 'venv','--seed','--python','3.12.2',uv_env_dir]))

# use the env we just made to install the executorch requirements
self.run(cmd_args_to_string([self.get_env_python(uv_env_dir),os.path.join(self.source_folder,'install_requirements.py')]),cwd=self.source_folder)

# add the env with the required packages to path
self.add_dir_to_path(self.get_env_bin_dir(uv_env_dir))

TareHimself avatar Nov 25 '25 21:11 TareHimself

Thanks very much for the feedback @TareHimself

That uv usage inside a pipenv sounds an interesting approach to be able to enforce a given python version. @davidsanfal is having a look at it, we might try to make something like this more built-in in Conan PipEnv.

memsharded avatar Dec 10 '25 11:12 memsharded