Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

adding a directory to sys.path

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


johnguenther at me

Nov 23, 2009, 9:02 AM

Post #1 of 3 (122 views)
Permalink
adding a directory to sys.path

This is Mac related. I am running snow leopard. I am using Python 2.6.3.

I had a lot of difficulty figuring out how to add a directory to sys.path that would be there every time I launched Idle. I have a directory called PythonPrograms in my Documents folder. When I installed Python it had '/Users/johnguenther/Documents' as sys.path[0]. I tried setting PYTHONPATH and this worked for running Python from the bash shell but only until I exited the shell. PYTHONPATH had to be reset each time I restarted the shell. And it had no effect on sys.path when I ran Idle.

I tried putting files with names ending with .pth in various directories all of which were already in sys.path. In this file I would put the path name of the directory, i.e., '/Users/johnguenter/Documents/PythonPrograms'. This had absolutely no effect.

I searched the web for help and found nothing that worked.

Finally I looked at a file that had been installed when I installed wxPython. I modified that file and now the directory I want is always part of sys.path when I start idle.

Since I had so much trouble finding an answer to the question "How do I permanently change sys.path?", I thought I might post this message.

I created a file called myconfig.pth

In this file I included one line:

import site; site.addsitedir('/Users/johnguenther/Documents/PythonPrograms')

I placed the file in /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages.
--
http://mail.python.org/mailman/listinfo/python-list


lists at cheimes

Nov 23, 2009, 10:15 AM

Post #2 of 3 (109 views)
Permalink
Re: adding a directory to sys.path [In reply to]

John Guenther schrieb:
> This is Mac related. I am running snow leopard. I am using Python 2.6.3.
>
> I had a lot of difficulty figuring out how to add a directory to sys.path that would be there every time I launched Idle. I have a directory called PythonPrograms in my Documents folder. When I installed Python it had '/Users/johnguenther/Documents' as sys.path[0]. I tried setting PYTHONPATH and this worked for running Python from the bash shell but only until I exited the shell. PYTHONPATH had to be reset each time I restarted the shell. And it had no effect on sys.path when I ran Idle.

See my blog posting:
http://lipyrary.blogspot.com/2009/08/how-to-add-new-module-search-path.html

Christian
--
http://mail.python.org/mailman/listinfo/python-list


paul.w.miller.please.dont.spam.me at wmich

Nov 23, 2009, 10:49 PM

Post #3 of 3 (104 views)
Permalink
Re: adding a directory to sys.path [In reply to]

On Mon, 23 Nov 2009 12:02:36 -0500, John Guenther wrote:

> This is Mac related. I am running snow leopard. I am using Python 2.6.3.
>
> I had a lot of difficulty figuring out how to add a directory to
> sys.path that would be there every time I launched Idle.
[...]

For a comprehensive discussion of how to modify sys.path, see

http://docs.python.org/install/index.html#modifying-python-s-search-path

If you really just need to modify sys.path for one particular user, what
I would do is add the line

export PYTHONSTARTUP="/home/my_user/python/startup.py"

to my .bashrc. Then, in the file /home/my_user/python/startup.py,
include the following code:

import sys

sys.path.append ("/home/my_user/some_dir")

This will add the directory /home/my_user/some_dir to sys.path every time
you start up Python.

The reason I like using PYTHONSTARTUP is that you can do other sorts of
customizations in that file, as well. For example, I hate not having any
way to clear the screen when I'm typing at the Python prompt, so I added
this code to my $PYTHONSTARTUP file:

import os

def clear():
os.system ('clear')

Hope that all helps!
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.