Page 1 of 1

no $PYTHONPATH environment variable?

Posted: Wed Sep 04, 2013 8:04 am
by farmwife
When I first fire up Konsole, 'echo $PYTHONPATH' returns a null string. Is that because there is no such environment variable unless Python is running?

I need a very simple BASH script that adds my personal code snippet library to any relevant path so that, whenever I run Python either interactively or by executing a script, I can import files from the code snippet library as if they were modules, no matter what my pwd happens to be. Sadly, I don't know enough BASH scripting to do that. :( Anyone wanna help?

Re: no $PYTHONPATH environment variable?

Posted: Wed Sep 04, 2013 8:29 am
by z1p101
Open your .profile file in your home directory with a text editor. Use View->Show Hidden Files if you have to.

Add the line "PYTHONPATH=/usr/bin/python" to the file (don't use quotes).

Save, logout, login again and it will be there.

One other thing, you can add "#!/usr/bin/python" (don't use quotes) to the top line of any file if you want it to execute using python.

Re: no $PYTHONPATH environment variable?

Posted: Wed Sep 04, 2013 8:53 am
by farmwife
I did as instructed and "echo $PYTHONPATH" still returns an empty string. This comment from that file might be helpful, but I don't understand it:

Code: Select all

# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.

# the default umask is set in /etc/profile; for setting the umask
# for ssh logins, install and configure the libpam-umask package.
#umask 022
And I was told to use "#! /usr/bin/env python" rather than "#! usr/bin/python". Why is the latter better?

Re: no $PYTHONPATH environment variable?

Posted: Wed Sep 04, 2013 10:03 am
by z1p101
Oops, sorry about that, add the line "PYTHONPATH=/usr/bin/python" to the file .bashrc and if the file isn't there just create it.
And I was told to use "#! /usr/bin/env python" rather than "#! usr/bin/python". Why is the latter better?
Not sure about the first one? I'm not a python expert but that may be for Mac or BSD or inside some other code or something. What you are doing is telling BASH where to find python for that file. Also, remember it is "#!/usr/bin/python" no quotes or spaces.

edit

What the comments at the top of the file are telling you is that there are other places to set variables depending on whether you want them to be gobal (all users) or user specific or used by bash or ... etc., etc.

Re: no $PYTHONPATH environment variable?

Posted: Wed Sep 04, 2013 11:03 am
by farmwife
Thanks. Now, how do I make my personal snippet library accessible to Python without having to import sys and use sys.append.path? My snippet library is in /home/[username]/python/userlib.

I also want to make python scripts in /home/[username]/python/bin executable from anywhere within my filesystem without having to type the full directory name. How do I do that?

Re: no $PYTHONPATH environment variable?

Posted: Wed Sep 04, 2013 6:10 pm
by z1p101
To add directories to your PATH you can add them to your .bashrc. for example you can add PATH=$PATH:/home/[username]/python/userlib.

Code: Select all

I also want to make python scripts in /home/[username]/python/bin executable from anywhere within my filesystem without having to type the full directory name. How do I do that?
I think you are making this more complicated then it needs to be. For example you can have two files...

useHello

Code: Select all

#!/bin/bash
hello.py
and the hello.py

Code: Select all

#!/usr/bin/python
print "Hello, World!"
As long as both are executable and in your PATH, useHello will call hello.py and execute it.