Skip to content

Commit

Permalink
Deprecate mixing Python environments using --paths=.../site-packages
Browse files Browse the repository at this point in the history
And add some XKCD-1987 themed *current python environment* hints into the
logs.
  • Loading branch information
bwoodsend committed May 28, 2024
1 parent e74f6e9 commit 75838e4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
1 change: 1 addition & 0 deletions PyInstaller/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ def run(pyi_args: list | None = None, pyi_config: dict | None = None):
logger.info('PyInstaller: %s, contrib hooks: %s', __version__, contrib_hooks_version)
logger.info('Python: %s%s', platform.python_version(), " (conda)" if compat.is_conda else "")
logger.info('Platform: %s', platform.platform())
logger.info('Python environment: %s', sys.prefix)

# Skip creating .spec when .spec file is supplied.
if args.filenames[0].endswith('.spec'):
Expand Down
16 changes: 14 additions & 2 deletions PyInstaller/building/build_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import pprint
import shutil
import enum

import re
import sys

from PyInstaller import DEFAULT_DISTPATH, DEFAULT_WORKPATH, HOMEPATH, compat
Expand Down Expand Up @@ -455,13 +455,25 @@ def __init__(
# Django hook requires this variable to find the script manage.py.
CONF['main_script'] = self.inputs[0]

for path in (pathex or []):
if pathlib.Path(path).name == "site-packages":
logger.log(
logging.DEPRECATION,
"Foreign Python environment's site-packages (%s) added to --paths/pathex. This is ALWAYS the wrong "
"thing to do. If your environment's site-packages is not in PyInstaller's module search path then "
"you are running PyInstaller from a different environment to the one your packages are in. Run "
"print(sys.prefix) without PyInstaller to get the environment you should be using then install and "
"run PyInstaller from that environment instead of this one. This warning will become an error in "
"PyInstaller 7.0.", path
)

self.pathex = self._extend_pathex(pathex, self.inputs)
# Set global config variable 'pathex' to make it available for PyInstaller.utils.hooks and import hooks. Path
# extensions for module search.
CONF['pathex'] = self.pathex
# Extend sys.path so PyInstaller could find all necessary modules.
logger.info('Extending PYTHONPATH with paths\n' + pprint.pformat(self.pathex))
sys.path.extend(self.pathex)
logger.info('Module search paths (PYTHONPATH):\n' + pprint.pformat(sys.path))

self.hiddenimports = hiddenimports or []
# Include hidden imports passed via CONF['hiddenimports']; these might be populated if user has a wrapper script
Expand Down
1 change: 1 addition & 0 deletions news/0.deprecation.2.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Adding a Python environment's ``site-packages`` directory to ``pathex``/``--paths`` will be blocked in v7.0

0 comments on commit 75838e4

Please sign in to comment.