I know how to set it in my /etc/profile and in my environment variables.
But what if I want to set it during a script?
Is it import os, sys? How do I do it?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You don’t set
PYTHONPATH, you add entries tosys.path. It’s a list of directories that should be searched for Python packages, so you can just append your directories to that list.In fact,
sys.pathis initialized by splitting the value ofPYTHONPATHon the path separator character (:on Linux-like systems,;on Windows).You can also add directories using
site.addsitedir, and that method will also take into account.pthfiles existing within the directories you pass. (That would not be the case with directories you specify inPYTHONPATH.)