简体   繁体   中英

How to undo sys.path.append(pathToModule)

I have been trying to fix the python path on my cpu, and I originally was just trying to change my .bash_profile , but that wasn't working, so I used

import sys
sys.pat.append(path/To/Module)

and now when I run my script, I get this error message

How can I either fix this issue or undo the sys.path.append(path/To/Module) ?

Also, is it possible to export multiple directories in the python path, and if so how do I do that?

Note that if you add a path with sys.path.append() you do this only for the current session. No need to undo it.

Just remove the line from you python file.

Have you tried sys.path.pop()
That will remove the last item that you added or indeed the last item on the PYTHONPATH, whatever that might be.

How can I either fix this issue or undo the >sys.path.append(path/To/Module)?

To undo the sys.path.append , you just need to remove that line from your script. Since the path is only modified for your current script and not system wide until you edit the PYTHONPATH .

Also, is it possible to export multiple directories in the python path, and if so how do I do that?

If you want to do it using sys , you can do it like: sys.path.extend(['/path1', '/path2'])

recently I found that pycharm ide will automatically append certain directory to your sys.path if you marked that directory as Sources Root. So even if you use sys.path.pop(sys.path.index(**directory path**) , it wouldn't work. Just unmark the folder and the issue will be solved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM