adding a directory to sys.path

J

John Guenther

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.
 
P

Paul Miller

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!
 
Joined
Jan 8, 2012
Messages
2
Reaction score
0
I just attempted the following:
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")
The problem is, it didn't work on my Debian Laptop. The sys.path used by IDLE refuses to use the paths that I specified. Anybody have any ideas?
 
Joined
Feb 15, 2012
Messages
1
Reaction score
0
This worked for me in Windows

I just attempted the following:

The problem is, it didn't work on my Debian Laptop. The sys.path used by IDLE refuses to use the paths that I specified. Anybody have any ideas?

Add an environmental variable PYTHONPATH

MyComputer -> Properties -> Advanced System Settings -> Environmental Variables

New ->
Name PYTHONPATH
Value C:\Python32\sample

Restart the IDLE

Hope this works for everyone using Python on Windows.

Ram
 
Joined
Jan 8, 2012
Messages
2
Reaction score
0
Add an environmental variable PYTHONPATH

MyComputer -> Properties -> Advanced System Settings -> Environmental Variables

New ->
Name PYTHONPATH
Value C:\Python32\sample

Restart the IDLE

Hope this works for everyone using Python on Windows.

Ram
Any idea how to do that in Debian Linux?
 
Joined
Jun 28, 2017
Messages
5
Reaction score
2
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

[...]

Hope that all helps!

I needed IDLE to be able to find the standard pip installed modules
so I could import things like numpy, or matplotlib, etc., so this is what I did:

sudo gedit /usr/local/lib/python3.6/site.py

change the line:

USER_SITE= NONE

to:

USER_SITE = '/usr/share'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,056
Messages
2,570,446
Members
47,097
Latest member
MarionMajo

Latest Threads

Top