PYTHONPATH

S

sushant.sirsikar

Hi,
I am using Linux env.I set the PYTHONPATH using

import sys
sys.path.append(----)

But we i close python and start again i is not showing my new entry in
PYTHONPATH.
Can anyone help me to make my path persistant?
Thanks
 
S

Sybren Stuvel

(e-mail address removed) enlightened us with:
I am using Linux env.I set the PYTHONPATH using

import sys
sys.path.append(----)

But we i close python and start again i is not showing my new entry in
PYTHONPATH.
Can anyone help me to make my path persistant?

Add the following to /etc/profile:

export PYTHONPATH="..."

Sybren
 
B

bruno at modulix

Hi,
I am using Linux env.I set the PYTHONPATH using

import sys
sys.path.append(----)

This does not sets the PYTHONPATH.

bruno@bousin ~/public_html/aquitaine-pqa $ python -h
(snip)
Other environment variables:
(snip)
PYTHONPATH : ':'-separated list of directories prefixed to the
default module search path. The result is sys.path.
(snip)

That is : it's the PYTHONPATH environnement variable that is used - if
defined - to set sys.path - not the other way round
But we i close python and start again i is not showing my new entry in
PYTHONPATH.

Of course.
Can anyone help me to make my path persistant?

Just like any other environnement variable on your system. With most
distros, it will be something like adding the line:

export PYTHONPATH="/a/possible/path;/another/one;/and/a/third"

either in /etc/profile (will be system-wide default) or ~/.bash_profile
(will be user-specific).

but this may vary according to your distro and your shell.
 
B

Brian van den Broek

bruno at modulix said unto the world upon 20/04/06 08:38 AM:

Just like any other environnement variable on your system. With most
distros, it will be something like adding the line:

export PYTHONPATH="/a/possible/path;/another/one;/and/a/third"

either in /etc/profile (will be system-wide default) or ~/.bash_profile
(will be user-specific).

but this may vary according to your distro and your shell.


Hi all,

reraising a slightly stale thread as I've got the same issue.

I'm a fairly recently convert to ubuntu from Windows and I'm still at
the stage where I am most comfortable with things right there for the
clicking, but I'm learning ;-)

The suggestions above appear not to work for me:

brian@Cedric:~/PythonFiles$ pwd
/home/brian/PythonFiles
brian@Cedric:~/PythonFiles$ cd ~
brian@Cedric:~$ cat /etc/profile | grep 'export PYTHONPATH'
export PYTHONPATH="~/PythonFiles"
brian@Cedric:~$ cat .bash_profile | grep 'export PYTHONPATH'
export PYTHONPATH="~/PythonFiles"
brian@Cedric:~$ python
Python 2.4.2 (#2, Sep 30 2005, 21:19:01)
[GCC 4.0.2 20050808 (prerelease) (Ubuntu 4.0.1-4ubuntu8)] on linux2
Type "help", "copyright", "credits" or "license" for more information..... if "PythonFiles" in p or "Numeric" in p:
.... print p
....
/usr/lib/python2.4/site-packages/Numeric
(I don't think it should matter, but I also tried the export line with
"/home/brian" in place of "~" in both files. I also tried with a ';'
trailing the single entry in my attempted PYTHONPATHs.)

I don't know where to look for more information; I'm assuming that
ubuntu isn't doing it the standard way bruno referred to above. Could
some ubuntu user cast light, please?

Thanks and best,

Brian vdB
 
E

Edward Elliott

Brian said:
The suggestions above appear not to work for me:

brian@Cedric:~$ cat /etc/profile | grep 'export PYTHONPATH'
export PYTHONPATH="~/PythonFiles"
brian@Cedric:~$ cat .bash_profile | grep 'export PYTHONPATH'
export PYTHONPATH="~/PythonFiles"

Those files are only read when you start the shell. I'm guessing you made
those changes from the currently running shell. To fix it you have 3 choices:
1. Close the shell and start a new one
2. Type 'bash -l' at the prompt to invoke a new login shell
3. Type '. .bash_profile' to read the changes into your current shell
(I don't think it should matter, but I also tried the export line with
"/home/brian" in place of "~" in both files.

Nope doesn't matter, as long as 'echo $HOME' is /home/brian.
I don't know where to look for more information; I'm assuming that
ubuntu isn't doing it the standard way bruno referred to above. Could
some ubuntu user cast light, please?

This information bruno gave is correct. It's not an ubuntu issue, it's the
way unix shells are designed to work. It's generally udnerstood that
changing environment variables in a profile requires forcing the shell to
reprocess them in one of the ways above. You can also type 'export
"VAR=value"' to set a variable in the current bash shell (but it's value
will be lost when the shell exits).

You might want to google for "bash shell tutorial" and familiarize yourself
with how bash shells work. It can be somewhat confusing at first, but it's
a very powerful way to interact with your system.
 
B

Brian van den Broek

Edward Elliott said unto the world upon 23/04/06 04:28 PM:
Those files are only read when you start the shell. I'm guessing you made
those changes from the currently running shell. To fix it you have 3 choices:
1. Close the shell and start a new one
2. Type 'bash -l' at the prompt to invoke a new login shell
3. Type '. .bash_profile' to read the changes into your current shell

Edward,

thanks for the reply. I'm going to not worry about exposing my
ignorance in what follows :)

I ought to have specified that I did indeed think to invoke a new
shell after I made the changes. However, trying (2) and (3) as you
specified shed some light.

Early on, (probably miss-)following something I read on the net, I'd put
a block into my .bash_profile that read:

if 1; then
somestuff

Trying (3) gave
brian@Cedric:~$ . .bash_profile
bash: 1: command not found

Commenting out the bad block in .bash_profile, if I open a shell and
things are somewhat better.

Only somewhat, as if I open a brand new shell:

brian@Cedric:~$ python
Python 2.4.2 (#2, Sep 30 2005, 21:19:01)
[GCC 4.0.2 20050808 (prerelease) (Ubuntu 4.0.1-4ubuntu8)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from sys import path
>>> path[0:3] ['', '/usr/lib/python24.zip', '/usr/lib/python2.4']
>>>
brian@Cedric:~$ . .bash_profile
brian@Cedric:~$ python
Python 2.4.2 (#2, Sep 30 2005, 21:19:01)
[GCC 4.0.2 20050808 (prerelease) (Ubuntu 4.0.1-4ubuntu8)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from sys import path
>>> path[0:3] ['', '/home/brian/PythonFiles', '/usr/lib/python24.zip']
>>>


So, it seems that I currently have to invoke '. .bash_profile'
manually with each new shell opened for this to have effect. (Trained
by Bill, I even rebooted to be sure that the invocation of
'. .bash_profile' is needed.)
But, still it is closer :)

I still haven't managed to coerce lines in /etc/profile exporting
PYTHONPATH to have an effect.



This information bruno gave is correct.

I didn't mean to suggest bruno was wrong, but instead that perhaps
ubuntu was a distro falling under his phrase:
but this may vary according to your distro and your shell

such that the instructions he gave would not work without
modification. Apologies if I gave a different impression.

You might want to google for "bash shell tutorial" and familiarize yourself
with how bash shells work. It can be somewhat confusing at first, but it's
a very powerful way to interact with your system.

Thanks for the suggestions. With luck, I'll get the remaining issue
sorted. Thanks for the help,

Brian vdB
 
E

Edward Elliott

Brian said:
if 1; then
somestuff

Trying (3) gave
brian@Cedric:~$ . .bash_profile
bash: 1: command not found

The error indicates the shell tried to execute a program named '1' and
couldn't find one. Arthimetic expressions generally have to be wrapped in
(()) in bash:

if ((1)); then
stuff

Conditional expressions use [[ ]] (the spaces inside the brackets matter).
So ((0)) is false while [[ 0 ]] is true. Yes the difference is screwy
and it sucks. Since ((1)) is always true, you could leave out the if line
altogether, but I'm guessing that 'stuff' is multiple lines that want to
toggle on and off.

Only somewhat, as if I open a brand new shell:

brian@Cedric:~$ python
from sys import path
path[0:3]
['', '/usr/lib/python24.zip', '/usr/lib/python2.4']

You realize this only prints the first 3 elements of path, right?
So, it seems that I currently have to invoke '. .bash_profile'
manually with each new shell opened for this to have effect.

Your shell must not be opening as a login shell. From the bash man page:

"When bash is invoked as an interactive login shell, or as a
non-interactive shell with the --login option, it first reads and executes
commands from the file /etc/profile, if that file exists. After reading
that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in
that order, and reads and executes commands from the first one that exists
and is readable...

"When an interactive shell that is not a login shell is started, bash
reads and executes commands from /etc/bash.bashrc and ~/.bashrc, if
these files exist."

I don't like differences between my interactive and login shell, so I
usually just link .bashrc to .bash_profile. You can do that with this
command (from your home dir):

ln .bash_profile .bashrc

(assuming .bashrc doesn't exist, if so remove it first). This makes
..bashrc the same file as .bash_profile, either name accesses the same contents.

Another option is to invoke one from the other with the . command. I.e.
put the line '. .bashrc' somewhere in .bash_profile and then move all the
common commands (like PYTHONPATH=) to .bashrc.

You shouldn't mess with /etc/profile for personal settings. That's what
..profile/.bashrc are for. /etc is for system-wide settings that apply to
all users on the system (including system processes, which you probably
don't want to affect).

(Trained by Bill, I even rebooted to be sure that the invocation of
'. .bash_profile' is needed.)

Rebooting is something you almost never need to do in linux unless you're
installing new hardware or a new kernel. It won't affect the operation of
bash in any way. That said, rebooting won't hurt either if you don't know
how to manually restart an affected process or daemon.

I still haven't managed to coerce lines in /etc/profile exporting
PYTHONPATH to have an effect.

As I said, must not be a login shell (see above).

I didn't mean to suggest bruno was wrong, but instead that perhaps
ubuntu was a distro falling ... such that the instructions he gave
would not work without modification. Apologies if I gave a different
impression.

Yes I got that. When you do need ubuntu-specific help, you can try the
forums on

http://ubuntuforums.org/

I don't think you'll need that for any Python-related questions though
(other than installing python packages with apt-get).
 
B

Brian van den Broek

Edward Elliott said unto the world upon 23/04/06 07:56 PM:
Brian van den Broek wrote:
Only somewhat, as if I open a brand new shell:

brian@Cedric:~$ python
from sys import path
path[0:3]
['', '/usr/lib/python24.zip', '/usr/lib/python2.4']


You realize this only prints the first 3 elements of path, right?

Indeed :) As the desired new path elements after running
'. .bash_profile' were inserted starting at path[1], I did this to cut
down on the size of the screen dump.
Your shell must not be opening as a login shell. From the bash man page:

When you do need ubuntu-specific help, you can try the
forums on

http://ubuntuforums.org/

I don't think you'll need that for any Python-related questions though
(other than installing python packages with apt-get).

Thanks again, Edward. If the information you've given me doesn't get
me sorted out, I will try with the ubuntu folk.

Best,

Brian vdB
 
B

Brian van den Broek

Hi all,

As a fairly new linux user running ubuntu 5.10 I'd had problems
persistently setting my PYTHONPATH environment variable. bruno and
Edward got me most of the way (thanks!); I'm posting what worked for
future googling.

bruno at modulix said unto the world upon 20/04/06 08:38 AM:
>
>
> Just like any other environnement variable on your system. With most
> distros, it will be something like adding the line:
>
> export PYTHONPATH="/a/possible/path;/another/one;/and/a/third"
>
> either in /etc/profile (will be system-wide default) or ~/.bash_profile
> (will be user-specific).

and


Edward Elliott said unto the world upon 23/04/06 07:56 PM:
Brian van den Broek wrote:



Your shell must not be opening as a login shell. From the bash man page:

"When an interactive shell that is not a login shell is started, bash
reads and executes commands from /etc/bash.bashrc and ~/.bashrc, if
these files exist."

So, to get the terminal ubuntu launches when you click on the panel's
terminal icon to have a customized PYTHONPATH, one must, as Edward
said, modify ~/.bashrc or link it with ~/.bash_profile.

The syntax that worked for me was *almost* what bruno suggested. It
didn't work here until I used ':' rather than bruno's ';' as the path
element separator. So, the lines

PYTHONPATH="/home/brian/FirstDir:/home/brian/SecondDir"
export PYTHONPATH

added to ~.bashrc make the terminal launched from the panel icon
include /home/brian/FirstDir and /home/brian/SecondDir right after pwd
in sys.path as desired.

Thanks Edward and bruno!

Best,

Brian vdB
 
E

Edward Elliott

Brian said:
It
didn't work here until I used ':' rather than ';' as the path
element separator.

Sorry, I missed the semi-colon before. Yes, unix uses colon to separate
path elements. Glad you figured it out and got it working.

FYI you can type 'man <command>' at the shell to get information about most
command-line programs (and a number of other things). That's where the
bash man page snippet I posted came from. Just in case you don't already
know.
 
B

bruno at modulix

Brian said:
Hi all,

As a fairly new linux user running ubuntu 5.10 I'd had problems
persistently setting my PYTHONPATH environment variable. bruno and
Edward got me most of the way (thanks!); I'm posting what worked for
future googling.
(snip)
The syntax that worked for me was *almost* what bruno suggested. It
didn't work here until I used ':' rather than bruno's ';' as the path
element separator.

oops, my bad :(
Sorry for the typo - it's of course ':'.
 

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
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top