pythonpath.rst 2.99 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
===============
The Python Path
===============

A collection of things to know when setting up the
:envvar:`PYTHONPATH` or diagnosing related problems.

What is the Python Path?
------------------------

.. envvar:: PYTHONPATH

It is an environment variable used by Python itself. It defines the
places from where to import Python modules. See `PYTHONPATH
<http://www.python.org/doc/current/install/index.html>`_

Here is how to see your :envvar:`PYTHONPATH`::

  $ python -c 'import sys; print sys.path'


The :xfile:`mypy` directory
---------------------------

Ac ommon practice is to create directory
:file:`~/mypy`
(or :file:`C:\\mypy` under Windows)
where you hold your local Python projects.
You may choose some other location, but it should be
a name without spaces and non-ascii characters.

Then you add this directory to your Python Path by saying in your `.bashrc` (or
whatever login script you use)::

  export PYTHONPATH = /home/mypy

(Under Windows you click around to find the place
where you can define *environment variables* and define
an environment variable :envvar:`PYTHONPATH` with the value ``C:\\mypy``).

The result is that any Python script in or below this directory
is now available as an importable Python module.

For example
if you create a file :file:`~/mypy/foo.py` with the following content...

::

  def hello():
      print "Hello, world!"

... then you can import a module ``foo`` from any Python process,
independently of the current directory::

  $ python
  Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) ...
  Type "help", "copyright", "credits" or "license" for more information.
  >\ >> import foo
  >\ >> foo.hello()
  Hello, world!
  >\ >> from foo import hello
  >\ >> hello()
  Hello, world!
  >\ >>

The :xfile:`local.pth` file
---------------------------

In your :xfile:`mypy` directory, create a file :xfile:`local.pth`,
a plain txt file that contains a list of directories
which Python should also add to its path.

Under Debian, this file should look like this::

  ~/snapshots/lino
  ~/snapshots/django-dev
  ~/snapshots/appy

Under Windows it should have the following content::

  c:\snapshots\lino
  c:\snapshots\python-dateutil
  c:\snapshots\appy
  c:\snapshots\Cheetah-2.4.4
  c:\snapshots\PyYAML-3.10\lib


Set the system-wide Python Path
-------------------------------

The following approach can be useful if you want to provide system users
with a development version of Django or some other Python package.
It requires you to have root permission.

To modify the system-wide Python Path,
add the file :xfile:`local.pth` to a directory that's already on
your Python Path:

=============== ==============================================
OS              Recommended directory
=============== ==============================================
Debian Lenny    :file:`/usr/local/lib/python2.5/site-packages`
Debian Squeeze  :file:`/usr/local/lib/python2.6/dist-packages`
Debian Wheezy   :file:`/usr/local/lib/python2.7/dist-packages`
=============== ==============================================