So I did some research and learn that you use the sys module to set the path for 2.5:

import sys
sys.path.append('usr/local/lib/python2.5/site-packages/')

And that would set the path for 2.5 permanently, but if i do the samething for 2.6 or 3.0, it will not work the next time I open the interpreter.

I thought this path was suppose to be set by default. it's not so i'm trying to set it.

help please
thanks

Recommended Answers

All 11 Replies

You can get the path to the site-packages directory like this

>>> import os
>>> os.path.join(os.path.split(os.__file__)[0], "site-packages")
'/usr/lib64/python2.6/site-packages'

With python 3:

>>> import os                                                   
>>> os.path.join(os.path.split(os.__file__)[0], "site-packages")
'/usr/local/lib/python3.1/site-packages'

(on the same machine).

However, you shouldn't normally need to put the site-packages in sys.path yourself. I'd like to see the value of your sys.path just after you start python.

You can get the path to the site-packages directory like this

>>> import os
>>> os.path.join(os.path.split(os.__file__)[0], "site-packages")
'/usr/lib64/python2.6/site-packages'

With python 3:

>>> import os                                                   
>>> os.path.join(os.path.split(os.__file__)[0], "site-packages")
'/usr/local/lib/python3.1/site-packages'

(on the same machine).

However, you shouldn't normally need to put the site-packages in sys.path yourself. I'd like to see the value of your sys.path just after you start python.

Doesn't even work temporarily.

"No module name file.py" after i did what you wrote and attempted to image a module

What I wrote doesn't do anything, it only computes the path to site-packages.
What you should do is paste your sys.path here (before you do anything with it).

What you should do is paste your sys.path here (before you do anything with it).

What do you mean by that? where is here?

What Gribouillis means is run this code and post the output here on DaniWeb ...

# show the system path for Python ( PYTHONPATH )

import sys
print( sys.path )

You need to run this with the version of Python you are interested in.

Also you cannot permanently set PYTHONPATH as it includes information about the current file you are working with.

I thought this path was suppose to be set by default. it's not so i'm trying to set it.

You can modify it on startup by adding PYTHONPATH to your ~/.bash.rc file, or create one if you don't have it.
export PYTHONPATH=$PYTHONPATH:/path/to/add:/second/path

>>> import sys
>>> sys.path

This is what i got. no site-pakcages though

I suggest that you write a module

# mystartup.py
import os, sys
site_packages = os.path.join(os.path.split(os.__file__)[0], "site-packages")
sys.path.append(site_packages)

and put this module somewhere on your python path. Then when you write a python program, you simply put

# at the top of your main program
import mystartup

This should work even if you have different versions of python.

Member Avatar for prouty

It's quicker to get the path to site-packages right from the command line:

python -c "from distutils.sysconfig import get_python_lib; print get_python_lib()"
from distutils.sysconfig import get_python_lib

Yes, I knew there was a function like this, but I couldn't remember where it was. Thanks.

That means that the startup code from Gribouillis could also be written this way ...

# mystartup.py
import sys
from distutils.sysconfig import get_python_lib
sys.path.append(get_python_lib())

Well, both ways ought to work equally well. :)

The advantage with the code from Gribouillis is that is somewhat self-documenting.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.