Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a directory to System path variable in Python [duplicate]

Tags:

python

I am running python 3.9 on Windows.

print(sys.path)
currDir = os.getcwd()
currDir += "\node"

sys.path.append(currDir)
print(sys.path)

I see the 2nd print out of sys.path has my c:\working\node\

But my issue is when I run the command under c:\working\node\, e.g. npm install

p = subprocess.Popen(["npm.cmd", "install]),

I get error saying 'The system cannot find the file specified'

And after my script is run , I try 'echo %PATH%', I don't see c:\working\node\ in the %PATH% varaible too?

Can you please tell me how can I add a new directory to system path variable so that subprocess.Popen can see the new addition?

like image 362
N Johnson Avatar asked Feb 05 '26 16:02

N Johnson


1 Answers

sys.path is not the same as the PATH variable specifying where to search for binaries:

sys.path

A list of strings that specifies the search path for modules. Initialized from the environment variable PYTHONPATH, plus an installation-dependent default.

You want to set the PATH variable in os.environ instead.

app_path = os.path.join(root_path, 'other', 'dir', 'to', 'app')
os.environ["PATH"] += os.pathsep + os.path.join(os.getcwd(), 'node')
like image 129
tari Avatar answered Feb 09 '26 10:02

tari



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!