9

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.

2
  • What do you mean? Do you mean to avoid that the working directory is added to it? Or do you want to break package imports? Anyway it sounds like your project is simply broken. You should refactor it to use a virtualenv. Commented Feb 10, 2019 at 22:12
  • I want the script's directory to NOT appear in sys.path. Like suggested by wpercy below, I could remove the dir (usually the first element of sys.path) from sys.path. I agree project is broken, unfortunately its not something I can simply refactor as this build system is grandfathered in. Commented Feb 11, 2019 at 1:03

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

3 Comments

this is what I'm doing now, wondering if there are any python interpreter flags or environment variables I can set to prevent the script dir from being put in the sys.path
the short answer is no, there's nothing you can pass to the interpreter that will prevent it from adding the script's own directory to sys.path. Python3 docs on "module search path"
for the question @fo_x86, based on the doc you can add -P flag: docs.python.org/3/using/cmdline.html#cmdoption-P

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.