how to set PATH=%PATH% as environment in Python script?
12,169
Solution 1
You can update PATH using several methods:
import sys
sys.path += ["c:\\new\\path"]
print sys.path
or
import os
os.environ["PATH"] += os.pathsep + os.pathsep.join(["c:\\new\\path"])
print os.environ["PATH"]
Solution 2
Repost Yaron's answer but dropped the sys.path. As in his post's comment, sys.path is for module search, not command search. PATH is for command search.
import os
os.environ["PATH"] += os.pathsep + os.pathsep.join(["c:\\new\\path"])
print os.environ["PATH"]
Author by
Admin
Updated on July 13, 2022Comments
-
Admin almost 2 years
I'am trying to start an exe file (the exe file is the output of c++ project compiled with visual studio) from a python program. In the properties of this c++ project (configuration ->properties-> debugging-> environment) the following setting in the
(PATH = %PATH%;lib\testfolder1;lib\testfolder2)
is given.
is there any way to set path environment variable to- PATH = %PATH%
- lib\testfolder1
- lib\testfolder2
in a python program?
Thanks in advance for your replay
-
Bob over 4 yearsIt is my understanding that
sys.path
relates to the Python module search path. It is not the same as the OS PATH environment variable. (In factsys.path
is associated with the PYTHONPATH environment variable.) -
user136036 over 4 yearsTo add current folder in which the .py file is in to path (for example if you use ctypes and have .dll files in it):
os.environ["PATH"] += os.pathsep + os.path.dirname(os.path.abspath(__file__))
. You have to run this before importing via ctypes. -
Nathaniel Ruiz over 2 yearsYour answer is correct, but I've submitted an edit to the top-voted answer in the meantime.