简体   繁体   中英

Adding to Python Path Dir

I have figured out how to temporarily append the path dir

import os, sys
sys.path.append('c:\\code')

... But how do I add a file permanently? I tried the following...

import os, sys
set PYTHONPATH=%PYTHONPATH%;C:\code

If you want to avoid setting environment variables, you can add a folder to your path by creating a .pth file in the folder %APPDATA%\\Python\\Python34\\site-packages (you may have to create the folder).

For example, with %APPDATA%\\Python\\Python34\\site-packages\\anything.pth containing the single line

c:\code

... and c:\\code containg the file woof.py with the following line:

MIAOW = "chirrup"

... we can check that it all works:

>>> from woof import MIAOW
>>> MIAOW
'chirrup'

I have found that the easiest way to set the environment variable is by following Computer > System Properties > Advanced System Settings > Environment Variables and editing PYTHONPATH manually. You can simply append it in the dialog box.

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