# Sys.path is included in the current directory by default

**URL:** <https://discuss.python.org/t/sys-path-is-included-in-the-current-directory-by-default/25272>\
**Category:** Python Help\
**Tags:** build, help\
**Created:** [March 29, 2023, 7:25am UTC](https://discuss.python.org/t/sys-path-is-included-in-the-current-directory-by-default/25272 "2023-03-29T07:25:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![zuhu2195](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/zuhu2195/32/10447_2.png) [@zuhu2195](https://discuss.python.org/u/zuhu2195)\
**Post date:** [March 29, 2023, 7:25am UTC](https://discuss.python.org/t/sys-path-is-included-in-the-current-directory-by-default/25272/1 "2023-03-29T07:25:41Z")

</div>

Hi,

I am trying a custom build of python 3.11 on linux, I see that sys.path has current directory by default, I would like to change this behavior by changing the cpython source,

```auto
>>> import sys
>>> sys.path
['', '/usr/local/lib/python311.zip', '/home/mohammed/python/Python-3.11.0/Lib', '/home/mohammed/python/Python-3.11.0/build/build/lib.linux-x86_64-3.11']
>>>

```

where can I make desired changes in Cpython source code such that my current directory is not included by default? Also is the behavior same for windows and other systems?

---

<div class="post-metadata">

**Author:** ![CAM-Gerlach](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/cam-gerlach/32/3688_2.png) [@CAM-Gerlach](https://discuss.python.org/u/CAM-Gerlach)\
**Post date:** [March 29, 2023, 8:36am UTC](https://discuss.python.org/t/sys-path-is-included-in-the-current-directory-by-default/25272/2 "2023-03-29T08:36:24Z")

</div>

There’s no need to change the CPython source; Python 3.11 added a new [-P command line option](https://docs.python.org/3/using/cmdline.html#cmdoption-P) and [`PYTHONSAFEPATH` environment variable](https://docs.python.org/3/using/cmdline.html#envvar-PYTHONSAFEPATH) to not add the CWD to `sys.path` by default. Therefore, simply invoke Python via `python -P` or with `PYTHONSAFEPATH=1` to get the behavior you’re looking for.

---

<div class="post-metadata">

**Author:** ![zuhu2195](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/zuhu2195/32/10447_2.png) [@zuhu2195](https://discuss.python.org/u/zuhu2195)\
**Post date:** [March 29, 2023, 8:45am UTC](https://discuss.python.org/t/sys-path-is-included-in-the-current-directory-by-default/25272/3 "2023-03-29T08:45:32Z")

</div>

I would not like to add -P every time I invoke python, is there any way to just invoke python with no cur dir in sys.path without using any options. I want to know if there is some change in configuration or change in Cpython source that I can make to get this behavior as default for my build.

---

<div class="post-metadata">

**Author:** ![CAM-Gerlach](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/cam-gerlach/32/3688_2.png) [@CAM-Gerlach](https://discuss.python.org/u/CAM-Gerlach)\
**Post date:** [March 29, 2023, 8:48am UTC](https://discuss.python.org/t/sys-path-is-included-in-the-current-directory-by-default/25272/4 "2023-03-29T08:48:02Z")

</div>

> [@zuhu2195](#):
>
> I want to know if there is some change in configuration

As mentioned, you can set the [`PYTHONSAFEPATH` environment variable](https://docs.python.org/3/using/cmdline.html#envvar-PYTHONSAFEPATH) in your shell/OS, however you normally set environment variables for your OS/shell. (E.g. `.bash_profile`, `.zshrc`, `Environment Variables` dialog on Windows, etc).

---

<div class="post-metadata">

**Author:** ![zuhu2195](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/zuhu2195/32/10447_2.png) [@zuhu2195](https://discuss.python.org/u/zuhu2195)\
**Post date:** [March 29, 2023, 9:03am UTC](https://discuss.python.org/t/sys-path-is-included-in-the-current-directory-by-default/25272/5 "2023-03-29T09:03:40Z")

</div>

Thank you Gerlach. I also tried this build on NonStop, I see some different behavior in NonStop systems, (they run on custom wrapper on top of linux OS). There sys.path doesn’t have cur directory by default. I did not find PYTHONSAFEPATH variable set there, neither did I use -P. so is this default behavior for non windows and non linux based systems?

---

<div class="post-metadata">

**Author:** ![CAM-Gerlach](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/cam-gerlach/32/3688_2.png) [@CAM-Gerlach](https://discuss.python.org/u/CAM-Gerlach)\
**Post date:** [March 29, 2023, 9:07am UTC](https://discuss.python.org/t/sys-path-is-included-in-the-current-directory-by-default/25272/6 "2023-03-29T09:07:11Z")

</div>

Windows behaves the same as macOS and Linux in this regard, at least with official [Python.org](http://Python.org) Python (and most other distributions I’m aware of). Given you’re using a rather non-standard proprietary OS, assuming you’re using the Python that is distributed with the system or by your system integrator or another third party, its possible that they’ve patched it to enable this by default, or otherwise it has something to do with the esoteric system architecture. You might want to ask them 🙂

---

<div class="post-metadata">

**Author:** ![zuhu2195](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/zuhu2195/32/10447_2.png) [@zuhu2195](https://discuss.python.org/u/zuhu2195)\
**Post date:** [March 29, 2023, 9:11am UTC](https://discuss.python.org/t/sys-path-is-included-in-the-current-directory-by-default/25272/7 "2023-03-29T09:11:13Z")

</div>

Thank you Gerlach.
