[Tutor] accessing modules found throughout a package?

Ben Finney ben+python at benfinney.id.au
Sun Oct 18 14:47:01 EDT 2015


Alex Kleider <akleider at sonic.net> writes:

> Should I add the following to the end of my ~/.bashrc file?
> export PYTHONPATH="$PYTHONPATH:/home/alex/Py"

No, because the entry should only be in PYTHONPATH *if* you want imports
to come from that package.

So the advice is:

* For the run-time application, it should be installed using the
  Distutils system (‘python3 ./setup.py install’) and hence its modules
  will be available on the path as normal.

  That is, the normal operation of the code will *not* be to run it from
  the development working tree, so that path should not be in your
  normal PYTHONPATH.

* THe ‘sys.path’ sequence is equivalent to the PYTHONPATH environment
  variable, and can be modified in your test suite's Python code as
  described elsewhere. But don't do that, because:

* Python will automatically add a ‘sys.path’ entry for the directory
  containing the script named on the command line. So this:

      $ cd ~/Projects/lorem/
      $ python3 ./setup.py test

  will run the ‘setup.py’ program with ‘sys.path’ already modified to
  contain the directory ‘/home/YOURUSERNAME/Projects/lorem’. Any
  packages in that directory are available for absolute import, *during*
  that test suite run.

* For this reason (and others), it's recommended to have ‘setup.py’ at
  the top level of your working tree. Put all the other Python code in
  packages and modules importable from the same location as ‘setup.py’,
  and run ‘setup.py’ from that directory to allow the ‘sys.path’
  modification to work as expected.

-- 
 \         “Alternative explanations are always welcome in science, if |
  `\   they are better and explain more. Alternative explanations that |
_o__) explain nothing are not welcome.” —Victor J. Stenger, 2001-11-05 |
Ben Finney



More information about the Tutor mailing list