python – adding directory to sys.path /PYTHONPATH

python – adding directory to sys.path /PYTHONPATH

This is working as documented. Any paths specified in PYTHONPATH are documented as normally coming after the working directory but before the standard interpreter-supplied paths. sys.path.append() appends to the existing path. See here and here. If you want a particular directory to come first, simply insert it at the head of sys.path:

import sys

That said, there are usually better ways to manage imports than either using PYTHONPATH or manipulating sys.path directly. See, for example, the answers to this question.

You could use:

import os
path = the path you want
os.environ[PATH] += :+path

python – adding directory to sys.path /PYTHONPATH

As to me, i need to caffe to my python path. I can add its path to the file
/home/xy/.bashrc by add

export PYTHONPATH=/home/xy/caffe-master/python:$PYTHONPATH.

to my /home/xy/.bashrc file.

But when I use pycharm, the path is still not in.

So I can add path to PYTHONPATH variable, by run -> edit Configuration.


Leave a Reply

Your email address will not be published. Required fields are marked *