473,739 Members | 7,912 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PYTHONPATH

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

Apr 20 '06 #1
10 6526
su************* *@gmail.com 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
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa
Apr 20 '06 #2
su************* *@gmail.com wrote:
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.
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom. gro'.split('@')])"
Apr 20 '06 #3
bruno at modulix said unto the world upon 20/04/06 08:38 AM:

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

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.
from sys import path
for p in path: .... if "PythonFile s" 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

Apr 23 '06 #4
Brian van den Broek wrote:
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.
Apr 23 '06 #5
Edward Elliott said unto the world upon 23/04/06 04:28 PM:
Brian van den Broek wrote:
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


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.

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


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.

<snip>

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

Apr 23 '06 #6
Brian van den Broek wrote:
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).
Apr 24 '06 #7
Edward Elliott said unto the world upon 23/04/06 07:56 PM:
Brian van den Broek wrote:
<snip>
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.
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:


<snip'ed many helpful things>
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
Apr 24 '06 #8
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:
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).


and
Edward Elliott said unto the world upon 23/04/06 07:56 PM: Brian van den Broek wrote:
<snip>
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:


<snip>
"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
Apr 24 '06 #9
Brian van den Broek wrote:
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.

Apr 24 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1488
by: Rami A. Kishek | last post by:
Hi, I would really appreciate help on this from all ye Win-oriented people. I have been using python under Linux for quite a while, know little about windows. I just upgraded my Python on WinME machine from Python 1.5.2 to 2.2.3 (in a separate directory). Using the Pythowin Pythonpath browser, I edited the python path to add my scripts directories. Then I tried to import my scripts and they're not visible. I tried many things: -...
2
26073
by: Eric Wichterich | last post by:
Hello Pythonistas, I am trying to get certain (self-written) libraries imported into my scripts using statements like "from library import function.py". But they are not being found. I think that the pythonpath-variable must be expanded to the location
4
1781
by: r.e.s. | last post by:
I have no PYTHONPATH nor any other python-related environment variables, yet everything seems fine. (I'm using PythonWin with winxp.) As long as modules are loaded through PythonWin, is PYTHONPATH unnecessary? Or am I missing something?
8
3045
by: Tero Pihlajakoski | last post by:
Hi, I've been experimenting on embedding Python to a C software, and ran into a little problem with PYTHONPATH (I'm running on linux). Here's the deal: When trying to call PyImport_Import("xyz"), Python returns an error "No module named xyz". The problem _seems_ to be that I had no PYTHONPATH variable defined (though python-shell works ok regardless), since the following in bash helps:
3
1584
by: D Denholm | last post by:
I recently installed Python 2.2 on my WinXP box. I am having problems figuring out how to create the PYTHONPATH correctly. I went to the WinXP SystemProperties > Advanced > Environment Variables and created a System Variable called PYTHONPATH with the value: G:\Python22\lib\site-packages\Pythonwin;
0
1756
by: Michael Yanowitz | last post by:
Hello: Someone on my team tried out installing my Python code and found that setting PYTHONPATH does not work, but setting PATH environment variable works the way PYTHONPATH should. Is that how it supposed to be, or is that a bug or feature? -----Original Message----- (parts deleted) Subject: question on Python for windows
3
1443
by: rh0dium | last post by:
Hi all, Can anyone help me out. I would like to have python automatically look in a path for modules similar to editing the PYTHONPATH but do it at compile time so every user doesn't have to do this.. Soo... I want to add /foo/bar to the PYTHONPATH build so I don't have to add it later on. Is there a way to do this?
4
6946
by: mhearne808[insert-at-sign-here]gmail[insert-dot-he | last post by:
I'm missing something major here. I'm trying to add a directory to my python path using the PYTHONPATH environment variable, and it's being ignored by the Python interactive shell. Below is a capture of what I did. Note that my newfolder appears nowhere on the list of directories in sys.path. How do I get Python to pay attention to my shell variables? Using bash on OS X 10.4.10.
1
1705
by: Aljosa Mohorovic | last post by:
i have a working MySQLdb module (/usr/lib/python2.4/site-packages/ MySQL_python-1.2.2-py2.4-linux-i686.egg), using it without problems. "clean shell" after login: python -c "import MySQLdb" reports no errors if i export PYTHONPATH: export PYTHONPATH=/var/www/projects/uv_portal/portal python -c "import MySQLdb" reports no errors as in previous case
0
8969
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9479
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8215
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6754
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6054
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4570
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4826
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2748
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.