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?

Ben McCormack
  • 32,086
  • 48
  • 148
  • 223
  • 4
    Appending it to your "PATH" or "PYTHONPATH" in your system environment variables (via the control panel) should work. – Benjamin Sep 19 '11 at 14:27
  • 7
    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'`! – Tim Pietzcker Sep 19 '11 at 14:37

6 Answers6

24

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 ";".

tiho
  • 6,655
  • 3
  • 31
  • 31
  • 1
    Thanks so much! I didn't realize that PYTHONPATH was a separate environment variable that I could edit! This did exactly what I needed. – Ben McCormack Sep 19 '11 at 21:49
  • 1
    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). – Mark Jan 10 '12 at 01:58
  • 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. – JYurkovich Jul 10 '19 at 17:35
  • JYTHONPATH for jython – GoTo Dec 27 '21 at 19:36
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 
Shaido
  • 27,497
  • 23
  • 70
  • 73
Artem Bulatov
  • 123
  • 3
  • 7
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.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • 3
    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? – Ben McCormack Sep 19 '11 at 14:45
  • Yep. Same here. Been wondering how you would actually do that? @Daniel Roseman – Chen Lizi May 29 '21 at 23:38
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.

Cédric Julien
  • 78,516
  • 15
  • 127
  • 132
  • modify using addsitedir(), .append() or .insert(); do not use = notation... otherwise it doesn't work! – Mark Jan 10 '12 at 01:54
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.

user3486773
  • 1,174
  • 3
  • 25
  • 50
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.

Spencer Rathbun
  • 14,510
  • 6
  • 54
  • 73