0

I wrote a small module that has this structure:

~/
   a/
      b/
         foo/
            __init__.py
            foo.py
            bar.py

Each time I use my module - I have to go in its folder and start my interactive session:

$ cd ~/a/b
$ ipython

In [0]: from foo import *

I would like to easily make available my module globally on my PC. Do I need to structure it as package with a setup.py and execute python setup.py install after each change on my module?

Another solution that I don't really like is adding the following to my ipython profile:

import sys
sys.path.append("~/a/b")

Or modifying my PYTHONPATH:

export PYTHONPATH=$PYTHONPATH:$HOME/a/b
boardrider
  • 5,882
  • 7
  • 49
  • 86
nowox
  • 25,978
  • 39
  • 143
  • 293
  • I think the last two options (adding new path to be searched during import) are best for you. What's bad about them? – Luke Dec 11 '15 at 14:45
  • Probably nothing bad about them. It was just a feeling there is always a better option than modifying my `user` config. – nowox Dec 11 '15 at 14:50
  • The only other option that comes to mind is to install a package and then modify those installed files. But it feels dirty. – Luke Dec 11 '15 at 15:00
  • Why not use `python setup.py develop`, then you don't need to reinstall after every change. – jonrsharpe Dec 13 '15 at 12:40

1 Answers1

0

Yes, install it globally as a python package or use a virtualenv

atx
  • 4,831
  • 3
  • 26
  • 40
  • 1
    What do you mean by `install it globally`? Does it mean I have to run `python setup.py install` each time I modify my module? – nowox Dec 11 '15 at 14:46
  • Yes, it means you would have to run that each time. By globally I assumed you were referring to your local python installation where libraries are able to be imported with the default PYTHONPATH. Mine will install here for example: '/usr/lib/python2.7/dist-packages'. Your best solution if you don't want to run setup.py every time is to use a virtualenv. – atx Dec 11 '15 at 14:49
  • So I have to read a bit about `virtualenv`. I've never used it. – nowox Dec 11 '15 at 14:51
  • 1
    It's very easy, the best guide is probably this one: http://docs.python-guide.org/en/latest/dev/virtualenvs/ – atx Dec 11 '15 at 14:51
  • In newer Python versions, you likely want venv instead of virtualenv: https://docs.python.org/3/library/venv.html – moorecm Dec 11 '15 at 15:00
  • Also, http://stackoverflow.com/questions/4383571/importing-files-from-different-folder-in-python may be a good read. – boardrider Dec 12 '15 at 13:00