Everything works as intended on my machine :)

Python 2.7.3 (default, Sep 26 2012, 21:51:14) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path.append('/home/sergey')
>>> sys.path
['', ..., '/home/sergey']
>>> sys.path.remove('/home/sergey')
>>> sys.path
['', ...]
>>> 

What exactly have you tried?

Regarding your understanding of things - I'm afraid there are some mis-understandings:

  1. sys.path is a list of directories which contain Python modules, not system libraries. So, simplifying, when you have something like import blah in your script, Python interpreter checks those directories one by one to check if there is a file called blah.py (or a subdirectory named blah with __init__.py file inside)

  2. Current directory is where the script is located, not where Python interpreter is. So if you have foo.py and bar.py in a directory, you can use import bar in foo.py and the module will be found because it's in the same directory.

  3. $PYTHONPATH is an environment variable which is getting appended to sys.path on interpreter startup. So, again, it is related to module search path and has nothing to do with starting Python from command line.

  4. Correct, you can modify sys.path at runtime - either when running a python script on in IDLE

See sys.path and site for more details.

Answer from Sergey on Stack Overflow
Top answer
1 of 3
63

Everything works as intended on my machine :)

Python 2.7.3 (default, Sep 26 2012, 21:51:14) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path.append('/home/sergey')
>>> sys.path
['', ..., '/home/sergey']
>>> sys.path.remove('/home/sergey')
>>> sys.path
['', ...]
>>> 

What exactly have you tried?

Regarding your understanding of things - I'm afraid there are some mis-understandings:

  1. sys.path is a list of directories which contain Python modules, not system libraries. So, simplifying, when you have something like import blah in your script, Python interpreter checks those directories one by one to check if there is a file called blah.py (or a subdirectory named blah with __init__.py file inside)

  2. Current directory is where the script is located, not where Python interpreter is. So if you have foo.py and bar.py in a directory, you can use import bar in foo.py and the module will be found because it's in the same directory.

  3. $PYTHONPATH is an environment variable which is getting appended to sys.path on interpreter startup. So, again, it is related to module search path and has nothing to do with starting Python from command line.

  4. Correct, you can modify sys.path at runtime - either when running a python script on in IDLE

See sys.path and site for more details.

2 of 3
0

We can try below to insert, append or remove from sys.path

>>> import sys
>>>
>>> sys.path.insert(1, '/home/log')
>>> sys.path.append('/home/log')
>>> sys.path
['', '/home/log']
>>> sys.path.remove('/home/log')
>>> sys.path
>>> ['']
>>>
🌐
Linus Tech Tips
linustechtips.com › software › programming
Python3 permanently remove path from sys.path - Programming - Linus Tech Tips
August 13, 2018 - I got this path that's my repo location and I want to remove it. sys.path.remove(path) works, but if I open a new python session it's back.. What can I do here?
Discussions

python - How to permanently remove value from sys.path? - Stack Overflow
wich operating system are you using? you must remove it from the user or machine env variables, not the session ones. ... Tsimp. Dim – Tsimp. Dim · 2017-05-21 09:11:14 +00:00 Commented May 21, 2017 at 9:11 ... You can append the path to $PATH, and not to $PYTHONPATH. More on stackoverflow.com
🌐 stackoverflow.com
Unintended removal of valid "sys.path" entries by isolation script
Environment data VS Code version: 1.45.1 Extension version (available under the Extensions sidebar): 2020.5.78807 OS and version: ArchLinux (May) Python version: 3.8.2 (ArchLinux) Type of virtual e... More on github.com
🌐 github.com
9
May 18, 2020
how to clean sys.path - Post.Byes - Bytes
Re: how to clean sys.path Thank you Laszlo! More on post.bytes.com
🌐 post.bytes.com
January 9, 2007
python - Remove entry from sys.path - Stack Overflow
After installing some Python Modules via homebrew, homebrew told me to run the following code. I don't need those modules anymore and want to cleanup everything. Can i remove (should i?) remove this More on stackoverflow.com
🌐 stackoverflow.com
🌐
Raspberry Pi Forums
forums.raspberrypi.com › board index › programming › python
Modify sys.path in Python3 - Raspberry Pi Forums
>>> import sys >>> sys.path ['', '/home/pi', '/usr/bin', '/usr/lib/python3.4', '/usr/lib/python3.4/plat-arm-linux-gnueabihf', '/usr/lib/python3.4/lib-dynload', '/usr/local/lib/python3.4/dist-packages', '/usr/lib/python3/dist-packages'] >>> sys.path.remove('/home/pi') >>> sys.path.insert(1,'/home/pi/Documents/Python Projects') >>> sys.path ['', '/home/pi/Documents/Python Projects', '/usr/bin', '/usr/lib/python3.4', '/usr/lib/python3.4/plat-arm-linux-gnueabihf', '/usr/lib/python3.4/lib-dynload', '/usr/local/lib/python3.4/dist-packages', '/usr/lib/python3/dist-packages'] >>> Path Browser shows sys.path before mods also.
🌐
Stack Overflow
stackoverflow.com › questions › 44095068 › how-to-permanently-remove-value-from-sys-path
python - How to permanently remove value from sys.path? - Stack Overflow
wich operating system are you using? you must remove it from the user or machine env variables, not the session ones. ... Tsimp. Dim – Tsimp. Dim · 2017-05-21 09:11:14 +00:00 Commented May 21, 2017 at 9:11 ... You can append the path to $PATH, and not to $PYTHONPATH.
🌐
GitHub
github.com › microsoft › vscode-python › issues › 11875
Unintended removal of valid "sys.path" entries by isolation script · Issue #11875 · microsoft/vscode-python
May 18, 2020 - After starting a debug session of a pytest test function, sys.path should contain all entires which are avialable if you would run pytest ... from a terminal. The isolation script does not only remove entries from sys.path which are related to VSCode, it will also remove one additional entry from sys.path which may break your debug session.
Author: microsoft
🌐
Post.Byes
post.bytes.com › home › forum › topic › python
how to clean sys.path - Post.Byes - Bytes
January 9, 2007 - > So far, I know only the command >>>sys.path.app end(r'c:....etc ...'), but how to delete or insert at the beginning of the list, I know not. > You can delete a slice. For example, del sys.path[2:5] More about slicing: http://docs.python.org/ref/slicings.html sys.path is a regular list.
Find elsewhere
🌐
Python
bugs.python.org › issue33053
Issue 33053: Avoid adding an empty directory to sys.path when running a module with `-m` - Python tracker
March 12, 2018 - This issue tracker has been migrated to GitHub, and is currently read-only. For more information, see the GitHub FAQs in the Python's Developer Guide · This issue has been migrated to GitHub: https://github.com/python/cpython/issues/77234
🌐
Reddit
reddit.com › r/learnpython › remove broken python dist from anaconda sys.path?
r/learnpython on Reddit: Remove broken python dist from Anaconda sys.path?
February 17, 2020 -

Hi, I have a few python versions installed on my Windows 10 system, alongside Anaconda. I'm trying to get python integration with Stata to work, and that seems to only work with the Anaconda installation of python, but there seems to be a piece of another distribution in the sys.path

>>> for path in sys.path:
...     print(path)
... 

C:\Program Files\Stata16\ado\base\py

C:\ProgramData\Anaconda3\python37.zip
C:\ProgramData\Anaconda3\DLLs
C:\ProgramData\Anaconda3\lib
C:\Program Files\Stata16
**C:\Users\akashian\AppData\Roaming\Python\Python37\site-packages**
C:\ProgramData\Anaconda3
C:\ProgramData\Anaconda3\lib\site-packages
C:\ProgramData\Anaconda3\lib\site-packages\win32
C:\ProgramData\Anaconda3\lib\site-packages\win32\lib
C:\ProgramData\Anaconda3\lib\site-packages\Pythonwin
.
C:\Program Files\Stata16\
C:\Program Files\Stata16\ado\base\
C:\Program Files\Stata16\ado\base\py
C:\Program Files\Stata16\ado\site\
C:\Program Files\Stata16\ado\site\py
c:\ado\plus\
c:\ado\plus\py
C:\Users\akashian\ado\personal\
C:\Users\akashian\ado\personal\py
c:\ado\

The bolded line breaks the importation of sklearn and other modules, I think because it's a 32 bit python version. How do I get rid of this, besides popping it off the sys.path list every time you run the python interpreter in Stata?

🌐
How to Use Linux
howtouselinux.com › home › understanding sys.path in python
understanding sys.path in Python - howtouselinux
October 9, 2025 - Second, sys.path is initialized when Python starts, so if you want to add or remove paths from sys.path , you’ll need to do it before importing any modules.
🌐
Java Guides
javaguides.net › 2024 › 12 › python-sys-path.html
Python sys path()
December 15, 2024 - import sys # Adding a new directory to the search path new_path = '/path/to/my/modules' sys.path.append(new_path) # Verifying the new path has been added print("Updated search path:") for path in sys.path: print(path) ... Updated search path: /usr/local/lib/python3.9 /usr/local/lib/python3.9/lib-dynload /usr/local/lib/python3.9/site-packages /path/to/my/modules ... This example demonstrates how to remove a directory from the search path using sys.path.remove.
🌐
Reddit
reddit.com › r/learnpython › how to delete files from multiple paths in python?
r/learnpython on Reddit: How to delete files from multiple paths in Python?
April 7, 2022 -

was following a tutorial on creating a Python script for deleting files that are older than a certain amount of days. I was able to successfully create the script and have it do what I want but now I need it to scan multiple paths not just the one. How would I go about doing that?

This is what I have:

import os
import datetime
import glob
import sys

path = r'PATHWAY'
logging_path = r'PATHWAY'

# Create log directory, wills skip if exists
if not os.path.isdir(logging_path):
    os.mkdir(logging_path)
else:
    print ("Directory already exists")

today = datetime.datetime.today() # Get current time
os.chdir(path) # Changing path to current path

# Create log file with timestamp
file=open(logging_path+datetime.datetime.today().strftime('%d-%m-%Y')+'.txt','a')

for root, directories, files in os.walk(path, topdown=False):
    for name in files:
        # Check last modified time
        t = os.stat(os.path.join(root, name))[8]
        filetime = datetime.datetime.fromtimestamp(t) - today

        # Is file older than 3 days?, If yes, delete.
        if filetime.days <= -3:
            print(os.path.join(root, name), filetime.days)
            file.write(os.path.join(root, name)+' created '+str(-1*filetime.days)+' days ago\n')
            os.remove(os.path.join(root, name))

This is what I tried doing:

import os
import datetime
import glob
import sys

path = [r'PATHWAY', r'PATHWAY2']
logging_path = r'PATHWAY'

# Create log directory, wills skip if exists
if not os.path.isdir(logging_path):
    os.mkdir(logging_path)
else:
    print ("Directory already exists")

today = datetime.datetime.today() # Get current time
os.chdir(path) # Changing path to current path

# Create log file with timestamp
file=open(logging_path+datetime.datetime.today().strftime('%d-%m-%Y')+'.txt','a')

for root, directories, files in os.walk(path, topdown=False):
    for name in files:
        # Check last modified time
        t = os.stat(os.path.join(root, name))[8]
        filetime = datetime.datetime.fromtimestamp(t) - today

        # Is file older than 3 days?, If yes, delete.
        if filetime.days <= -3:
            print(os.path.join(root, name), filetime.days)
            file.write(os.path.join(root, name)+' created '+str(-1*filetime.days)+' days ago\n')
            os.remove(os.path.join(root, name))

After trying this though I get an error about chdir on line 15. I'm clearly missing something but I don't know enough about Python or programming to figure that out yet.

🌐
KK's Blog
fromkk.com › posts › sys-dot-path-in-python
Sys.path in Python - KK's Blog (fromkk)
June 4, 2026 - By default, as initialized upon ... python (REPL): prepend an empty string, which means the current working directory. You can remove these path with -P param....
🌐
Tutorialspoint
tutorialspoint.com › python › os_remove.htm
Python os.remove() Method
Python method remove() of OS module accepts a path to the file we want to delete. This path can be an absolute or relative path and can be provided as a string or bytes object.