how to set different PYTHONPATH variables for python3 and python2 respectively

后端 未结 4 1425
情书的邮戳
情书的邮戳 2021-01-30 10:39

I want to add a specific library path only to python2. After adding export PYTHONPATH=\"/path/to/lib/\" to my .bashrc, however, executing python3 gets

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-30 11:00

    I found that there is no way to modify PYTHONPATH that is only for python2 or only for python3. I had to use a .pth file.

    What I had to do was:

    • make sure directory is created in my home: $HOME/.local/lib/python${MAJOR_VERSION}.${MINOR_VERSION}/site-packages
    • create a .pth file in that directory
    • test that your .pth file is work
    • done

    For more info on `.pth. file syntax and how they work please see: python2 docs and python3 docs.

    (.pth files in a nutshell: when your python interpreter starts it will look in certain directories and see the .pth file, open those files, parse the files, and add those directories to your sys.path (i.e. the same behavior as PYTHONPATH) and make any python modules located on those directories available for normal importing.)

提交回复
热议问题