1

In the process of coding, I often make use of one or more packages of my own that I'm in the process of evolving. These packages are not located in a directory that is part of my Python installation, and are thus not on my default path for locating packages.

What is the best way to ensure access to a specific one of these packages in other code I'm working on? One approach I've taken is

import sys
sys.path.append('/Users/Rax/Documents/Projects/Coding/Python/mypackages')
import somepackage

which at least contains the effect of the package to the place where it us needed. But this seems cumbersome and brittle.

Is there a generally accepted best practice, or perhaps some feature I'm missing, for ensuring a locally developed package is found? Or am I overthinking this: should I simply put add the path to my packages to the global packages path?


This is not a virtualenv question.

orome
  • 45,163
  • 57
  • 202
  • 418
  • You could place a .pth file with a link to the module in site-packages – M4rtini Dec 19 '13 at 22:15
  • 3
    Or add your own package's path to your `PYTHON_PATH` env variable. – bruno desthuilliers Dec 19 '13 at 22:21
  • Yes you are overthinking this. ;) You posted the answer in your own question. – Raydel Miranda Dec 19 '13 at 22:23
  • 1
    How is this not a `virtualenv` question? For something like this, I usually use a `virtualenv`, then install the package in development mode (`pip install -e`). Modifying `sys.path` in your code is brittle; everything revolves around loading your bootstrap code at the right time. Better to just have the package installed from the get go and be done with it. Alternatively, you could set a `PYTHONPATH` (which again, you could ease the use of by setting in a `virtualenv` `activate` script), but keeping everything routed through your package manager tends to make life easier in the long run. – Silas Ray Dec 19 '13 at 22:37
  • @SilasRay: Because I'm asking how to do it without using `virtualenv`. – orome Dec 19 '13 at 22:44
  • But why? Do you have a reason why it won't work for you? It seems like an arbitrary constraint you are imposing to exclude exactly the tool that would work best. – Silas Ray Dec 19 '13 at 22:47
  • @SilasRay: In every case I've encountered (maybe this is for meta) it has turned out that `virtualenv` is not in fact the simple panacea it would seem to be. But I'm willing to hear the details as an example. Remember the workflow here is one where I've probably got one window where I'm editing a some code that uses my package, and another where I'm actively editing my package, and meanwhile I have all the things in site-packages that I always want everything I use to have access to. I'll entertain a `virtualenv` answer that covers that workflow. – orome Dec 19 '13 at 22:57
  • You may find my [answer describing a basic Python development process](http://stackoverflow.com/a/19877478/1599111) somewhat related to your question - particulary the section about installing a package as a development egg. That doesn't depend on you using virtualenv. – Lukas Graf Dec 19 '13 at 23:12

0 Answers0