Skip to content

Conversation

@davidsanfal
Copy link
Contributor

@davidsanfal davidsanfal commented Oct 2, 2025

Changelog: Fix: Improve the python virtualenv creation using the python installed in the machine.
Docs: TODO

  • Refer to the issue that supports this Pull Request.
  • If the issue has missing info, explain the purpose/use case/pain/need that covers this Pull Request.
  • I've read the Contributing guide.
  • I've followed the PEP8 style guides for Python code.
  • I've opened another PR in the Conan docs repo to the develop branch, documenting this one.

@davidsanfal davidsanfal force-pushed the feature/pyinstaller-venv branch from 237122f to 0501a01 Compare October 3, 2025 10:14
@davidsanfal davidsanfal force-pushed the feature/pyinstaller-venv branch from 0501a01 to 57efa02 Compare October 3, 2025 10:27
@davidsanfal davidsanfal marked this pull request as ready for review October 4, 2025 09:01
@davidsanfal davidsanfal requested review from czoido and jcar87 October 6, 2025 17:19
env.prepend_path("PATH", self.bin_dir)
env.vars(self._conanfile).save_script(self.env_name)

def _create_venv(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about relying on a user configuration pointing to the python interpreter when not found in path?

"tools.system.package_manager:mode": "Mode for package_manager tools: 'check', 'report', 'report-installed' or 'install'",
"tools.system.package_manager:sudo": "Use 'sudo' when invoking the package manager tools in Linux (False by default)",
"tools.system.package_manager:sudo_askpass": "Use the '-A' argument if using sudo in Linux to invoke the system package manager (False by default)",
"tools.system.pip_manager:python_interpreter": "Path to the Python interpreter to be used to create the virtualenv",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"tools.system.pip_manager:python_interpreter": "Path to the Python interpreter to be used to create the virtualenv",
"tools.system.pipenv:python_interpreter": "Path to the Python interpreter to be used to create the virtualenv",

what about tools.system.pipenv:python_interpreter ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used the same name as the python module, thinking it was our "naming standard". If we don't follow that pattern, I'll change it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say we don't have have a naming standard, In my opinion the most important rule is that is easy to find for users and the name is a bit self-explanatory to the extent possible? Check other confs to see if you think it could work

@davidsanfal davidsanfal added this to the 2.22.0 milestone Oct 22, 2025
def _create_venv(self):
python_interpreter = self._conanfile.conf.get(
"tools.system.pipenv:python_interpreter",
default=shutil.which('python') if platform.system() == "Windows" else shutil.which('python3'))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the shutil.which() default to find the current interpreter if we are running in a venv already? Or it finds it directly in the system?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the one inside the virtualenv. But it's a python that's only used during recipe execution. Every time you run it, it's a new environment. In any case, I'm going to add that it gets the real path because in virtualenv python is a symlink.

python_interpreter = self._conanfile.conf.get("tools.system.pipenv:python_interpreter",
default=os.path.realpath(default_python) if default_python else None)
if not python_interpreter:
raise ConanException("PipEnv could not find a Python executable path.")
Copy link
Contributor

@czoido czoido Oct 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the error should give the user some hints about the solution. Something like this?

Could not find a usable Python interpreter to create a virtualenv.
Install Python system-wide or set the "tools.system.pipenv:python_interpreter" conf to the full path of a Python executable.

Comment on lines +28 to +32
default_python = shutil.which('python') if platform.system() == "Windows" else shutil.which('python3')
python_interpreter = self._conanfile.conf.get("tools.system.pipenv:python_interpreter",
default=os.path.realpath(default_python) if default_python else None)
if not python_interpreter:
raise ConanException("PipEnv could not find a Python executable path.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why doing shutil.which before checking the conf? I think it should first check the conf and then, if not defined go for shutil.which

"""

venv.EnvBuilder(clear=True, with_pip=True).create(self._env_dir)
self._create_venv()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should have at least a basic test testing with mocks that:

  • The conf has priority
  • The fallback depending on the OS
  • Error message when no interpreter is found

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants