no $PYTHONPATH environment variable?

All Gurus once were Newbies
Forum rules
There are no such things as "stupid" questions. However if you think your question is a bit stupid, then this is the right place for you to post it. Please stick to easy to-the-point questions that you feel people can answer fast. For long and complicated questions prefer the other forums within the support section.
Before you post please read how to get help
Post Reply
farmwife
Level 1
Level 1
Posts: 9
Joined: Mon Sep 02, 2013 11:37 pm

no $PYTHONPATH environment variable?

Post 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?
User avatar
z1p101
Level 1
Level 1
Posts: 31
Joined: Tue Jul 24, 2012 7:29 pm

Re: no $PYTHONPATH environment variable?

Post 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.
farmwife
Level 1
Level 1
Posts: 9
Joined: Mon Sep 02, 2013 11:37 pm

Re: no $PYTHONPATH environment variable?

Post 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?
User avatar
z1p101
Level 1
Level 1
Posts: 31
Joined: Tue Jul 24, 2012 7:29 pm

Re: no $PYTHONPATH environment variable?

Post 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.
farmwife
Level 1
Level 1
Posts: 9
Joined: Mon Sep 02, 2013 11:37 pm

Re: no $PYTHONPATH environment variable?

Post 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?
User avatar
z1p101
Level 1
Level 1
Posts: 31
Joined: Tue Jul 24, 2012 7:29 pm

Re: no $PYTHONPATH environment variable?

Post 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.
Post Reply

Return to “Newbie Questions”