Export PYTHONPATH - syntax error

48,376

Solution 1

If you really mean you are typing

 >>> export PYTHONPATH...

in the Python "interactive shell", the syntax error is because it is not valid Python, it is a command (bash) shell statement:

 $ export PYTHONPATH="$PYTHONPATH:/where/module/lives/"
 $ python
 Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
 >>> import MySQLdb
 >>>

Solution 2

If you want to modify the path to packages from within Python, you can do:

import sys
sys.path.append('/where/module/lives/')

The syntax export PYTHONPATH=… is understood by (Bourne) shells (bash, etc.).

Both uses have their advantage:

  • For modules that are not used often, the "within" Python approach is often best, since you do not have to pollute PYTHONPATH with the path to all minor modules.
  • For modules that are used in many programs, the shell approach is often best; in this case, you can permanently modify PYTHONPATH by updating it in you shell initialization file (.bashrc, etc.).

Solution 3

If you want the change to be permanent , then append this line in ~/.bashrc

 export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python2.7/site-packages
Share:
48,376

Related videos on Youtube

Kalinin
Author by

Kalinin

Updated on August 28, 2020

Comments

  • Kalinin
    Kalinin over 3 years

    I need to connect MySQLdb - module.

    I download MySQLdb - module and install it.

    But when i write (in python interactive shell): import MySQLdb - i get no module named MySQLdb.

    Then i decided to include MySQLdb directory in PYTHONPATH variable.

    I write (in python interactive shell): export PYTHONPATH=${PYTHONPATH}:/where/module/lives/

    And in response i receive a syntax error: invalid syntax: export PYTHONPATH^=${PYTHONPATH}:/where/module/lives/

    What's wrong with syntax here?

  • Cam Jackson
    Cam Jackson over 10 years
    Should the colon in that bash statement be a semicolon? Don't you want /some/path to become /some/path;/where/the/module/lives/?
  • msw
    msw over 10 years
    Semi-colons are path separators on Windows and colons do the job on Unix. The question used Unix syntax even though the OP didn't explicitly say what OS was being used.
  • Cam Jackson
    Cam Jackson over 10 years
    Just when I thought I was getting OK at Linux, there's something really basic I didn't know!

Related