Add a directory to Python sys.path so that it's included each time I use Python
Asked Answered
K

6

18

Currently, when trying to reference some library code, I'm doing this at the top of my python file:

import sys
sys.path.append('''C:\code\my-library''')
from my-library import my-library

Then, my-library will be part of sys.path for as long as the session is active. If I start a new file, I have to remember to include sys.path.append again.

I feel like there must be a much better way of doing this. How can I make my-library available to every python script on my windows machine without having to use sys.path.append each time?

Ketonuria answered 19/9, 2011 at 14:22 Comment(2)
Appending it to your "PATH" or "PYTHONPATH" in your system environment variables (via the control panel) should work.Ootid
Also, you should use raw strings instead of multiline strings when you're writing directory names on Windows (because of the unfortunate backslashes): r'C:\code\my-library'!Wenz
D
25

Simply add this path to your PYTHONPATH environment variable. To do this, go to Control Panel / System / Advanced / Environment variable, and in the "User variables" sections, check if you already have PYTHONPATH. If yes, select it and click "Edit", if not, click "New" to add it.

Paths in PYTHONPATH should be separated with ";".

Dene answered 19/9, 2011 at 15:12 Comment(4)
Thanks so much! I didn't realize that PYTHONPATH was a separate environment variable that I could edit! This did exactly what I needed.Ketonuria
this works great for local installations where you can set environment variables. however, use the sitecustomize.py method for stand alone network-based 'installs'/deployments (see below for Cedric Julien's description).Hellbent
I was getting an unhandled win32 exception for spssengine.exe when I was trying to run Python code in SPSS statistics 22 and setting PYTHONPATH to "C:\Program Files\Python27\Lib\site-packages" fixed the problem.Hypotrachelium
JYTHONPATH for jythonLavation
S
7
  1. You should use os.path.join to make your code more reliable.
  2. You have already used __my-library__ in the path. So don't use it the second time in import. If you have a directory structure like this C:\code\my-library\lib.py and a function in there, e.g.:
def main():
  print("Hello, world")

then your resulting code should be

import sys 
sys.path.append(os.path.join('C:/', 'code', 'my-library'))

from lib import main 
Shirlyshiroma answered 26/8, 2019 at 11:56 Comment(0)
B
4

If this is a library that you use throughout your code, you should install it as such. Package it up properly, and either install it in your site-packages directory - or, if it's specific to certain projects, use virtualenv and install it just within the relevant virtualenvs.

Brigettebrigg answered 19/9, 2011 at 14:34 Comment(2)
I figure this is probably the best approach. I'm really new to Python development. Could you point me in the right direction on how to do this?Ketonuria
Yep. Same here. Been wondering how you would actually do that? @Daniel RosemanMedor
H
3

To do such a thing, you'll have to use a sitecustomize.py (or usercustomize.py) file where you'll do your sys.path modifications (source python docs).

Create the sitecustomize.py file into the \Lib\site-packages directory of your python installation, and it will be imported each time a python interpreter is launched.

Handoff answered 19/9, 2011 at 14:29 Comment(1)
modify using addsitedir(), .append() or .insert(); do not use = notation... otherwise it doesn't work!Hellbent
S
0

If you are doing this interactively, the best thing to do would be to install ipython and configure your startup settings to include that code. If you intend to have it be part of a script you run from the interpreter, the same thing applies, since it will have access to your namespace.

On the other hand, a stand alone script should not include that automatically. In the future, you or some other maintainer will come along, and all the code should be obvious, and not dependent upon a specific machine setup. The best thing to do would be to set up a skeleton file for new projects that includes all of the basic functionality you need. That, along with oft-used snippets will handle the problem.

All of your code to run the script, will be in the script, and you won't have to think about adding that code every time.

Sherlocke answered 19/9, 2011 at 14:29 Comment(0)
V
0

Using jupyter with multiple environments, adding the path to .bashrc didn't work. I had to edit the kernel.json file for that particular kernel and append it to the PYTHONPATH in env section.

This only worked in that kernel but maybe this can help someone else.

Veiling answered 23/2, 2021 at 3:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.