How to add to beginning Python path in bash
-
26-10-2019 - |
سؤال
Here is my .bash_profile
PYTHONPATH=".:/home/miki725/django/django:$PYTHONPATH"
export PYTHONPATH
So then I open python however the directory I add in .bash_profile
is not the first one:
Python 2.4.3 (#1, Sep 21 2011, 20:06:00)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-51)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> for i in sys.path:
... print i
...
/usr/lib/python2.4/site-packages/setuptools-0.6c9-py2.4.egg
/usr/lib/python2.4/site-packages/flup-1.0.2-py2.4.egg
/usr/lib/python2.4/site-packages/MySQL_python-1.2.3c1-py2.4-linux-i686.egg
/usr/lib/python2.4/site-packages/django_form_utils-0.1.7-py2.4.egg
/usr/lib/python2.4/site-packages/mechanize-0.2.1-py2.4.egg
/usr/lib/python2.4/site-packages/Django-1.2.1-py2.4.egg
/usr/lib/python2.4/site-packages/mercurial-1.6-py2.4-linux-i686.egg
/usr/lib/python2.4/site-packages/lxml-2.2.7-py2.4-linux-i686.egg
/usr/lib/python2.4/site-packages/django_registration-0.7-py2.4.egg
/usr/lib/python2.4/site-packages/sorl_thumbnail-3.2.5-py2.4.egg
/usr/lib/python2.4/site-packages/South-0.7.2-py2.4.egg
/usr/lib/python2.4/site-packages/django_keyedcache-1.4_1-py2.4.egg
/usr/lib/python2.4/site-packages/django_livesettings-1.4_3-py2.4.egg
/usr/lib/python2.4/site-packages/django_app_plugins-0.1.1-py2.4.egg
/usr/lib/python2.4/site-packages/django_signals_ahoy-0.1_2-py2.4.egg
/usr/lib/python2.4/site-packages/pycrypto-2.3-py2.4-linux-i686.egg
/usr/lib/python2.4/site-packages/django_threaded_multihost-1.4_0-py2.4.egg
/usr/lib/python2.4/site-packages/PIL-1.1.7-py2.4-linux-i686.egg
/usr/lib/python2.4/site-packages/pyOpenSSL-0.11-py2.4-linux-i686.egg
/usr/lib/python2.4/site-packages/ZSI-2.0_rc3-py2.4.egg
/usr/lib/python2.4/site-packages/PyXML-0.8.4-py2.4-linux-i686.egg
/usr/lib/python2.4/site-packages/pyquery-0.6.1-py2.4.egg
/usr/lib/python2.4/site-packages/pip-1.0.1-py2.4.egg
/usr/lib/python2.4/site-packages/virtualenv-1.6.1-py2.4.egg
/usr/lib/python2.4/site-packages/simplejson-2.1.6-py2.4-linux-i686.egg
/home/miki725
/home/miki725/django/django
/usr/lib/python24.zip
/usr/lib/python2.4
/usr/lib/python2.4/plat-linux2
/usr/lib/python2.4/lib-tk
/usr/lib/python2.4/lib-dynload
/usr/lib/python2.4/site-packages
/usr/lib/python2.4/site-packages/Numeric
/usr/lib/python2.4/site-packages/PIL
/usr/lib/python2.4/site-packages/gtk-2.0
>>>
>>>
>>>
>>>
>>> import django
>>> django.__file__
'/usr/lib/python2.4/site-packages/Django-1.2.1-py2.4.egg/django/__init__.pyc'
>>>
How can I add to a python path in .bash_profile
so it would be in the beginning. This is for shared hosting. I need to be able to import my django install instead of using system default.
Thank you
المحلول
Your best bet is to modify sys.path
at runtime. In a shared hosting enviroment it's common to do this in your .wsgi file. You could do something like this:
import sys
sys.path.insert(0, '/home/miki725/django/django')
If you add export PYTHONSTARTUP=/home/miki725/.pythonrc
to your .bash_profile
, you can add that your .pythonrc
file, and it'll be executed before an interactive prompt is shown as well.
نصائح أخرى
As an alternative approach, you could modify sys.path
directly from the interpreter:
sys.path.insert(0,"/home/miki725/django/django")
I'd say that your PYTHONPATH
is being modified when the site module is imported. Please have a look at the user module to provide user-specific configuration (basically just prepend the directories you're interested in to sys.path
).
Note: user
module is currently deprecated, but for python 2.4 this should work.
Edit: Just for completeness, for python >= 2.6 (user
module deprecated), you should create a usercustomize.py
file in your local site-packages
directory as explained here.
As indicated by others, you modify the sys.path
directly in Python like this:
sys.path.insert(0,"/home/miki725/django/django")
But I think that virtualenv is the solution you are looking for. This tool allows you to create isolated Python environments.