Prevent script dir from being added to sys.path in Python 3

Question:

Is there a way to prevent the script’s directory from being added to sys.path in python3? I’m getting import conflicts due to the fact that imports are relative in python. A legacy project I’m working with has a file called logger.py in the root directory of the script which conflicts with the built-in logger.

The custom build system that I use ends up creating symlinks to all the files and dependencies and in production, at runtime we use the -E flag to ignore any system set PYTHONPATH and set the path to what we want. But running tests/scripts from PyCharm doesn’t work because of this conflict.

Asked By: fo_x86

||

Answers:

At the top of your script, you can try doing something like:

import os
import sys

# the first element of sys.path is an empty string, meant to represent the current directory
sys.path.remove('')

then do your normal imports.

Beware, this will cause all relative imports from your current directory to fail, potentially causing more problems than your legacy logger.py

With regards to your second question, whether or not there’s anything that can be done to prevent the directory from being added to sys.path in the first place, the short answer is no. From the Python 3 docs on “module search path” :

sys.path is initialized from these locations:

  • The directory containing the input script (or the current directory when no file is specified).
  • PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).
  • The installation-dependent default.

I suppose you could set up a symlink from your current working directory to another directory, keep your actual script there, and point the symlink at it. Also from the above docs (emphasis mine):

On file systems which support symlinks, the directory containing the input script is calculated after the symlink is followed.

Answered By: wpercy

The -I option for isolated mode was added in Python 3.4. In the isolated mode the script directory is not added to sys.path, but there are also other restrictions such as ignoring the user’s site-packages directory (-s option), and all the PYTHON* environment variables (-E option).

Python 3.11 has a -P option and a PYTHONSAFEPATH variable which prevent prepending the script directory or other potentially unsafe paths to the sys.path.

In Windows a ._pth file can be used to completely override the sys.path and activate the isolated mode. This technique is used in the Windows embeddable package.

Answered By: u18056049
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.