[Edu-sig] PEPTALK: path sanity and newbie mental health -- please help

Jason Cunliffe Jason Cunliffe" <jason.cunliffe@verizon.net
Sun, 6 Jul 2003 14:37:07 -0400


Greetings

I've been away from Python for while and now diving back, hoping to
experiment
with Leo and Jabber over the summer...

*Context*
I am humiliated and feel like moron. I am getting stuck [again] on my most
basic eternal Python newbie issue: paths and module import.
I must be missing something. I consult some excellent books, then googled
further into it, but found I was taken on a pythonesque journey which did
not help either. However I met trails of others experiencing similar
confusion. I imagine Python teachers and students must have dealt with this
satisfactorily gazillion times, but still am compelled to think there must
be a better simpler way.
I'd really appreciate your help and feedback on my suggestions.

Hopefully this will end up as a PEP which makes life easier for all,
especially beginners. btw, I tend to agree with Kirby's recent comment:

"What does or doesn't make sense to the newbie is irrelevant.  A language
need not be designed based on that
perspective, to be good.  It's the job of teachers to smooth the on ramp."

But I 'd add to that configuration and preferences *are* the universal
'on-ramp' and need to be designed with beginners in mind as well as advanced
professionals. I believe those two goals cannot be satisfied in one simple
consistent solution. Python fails at present to achieve this IMHO,
[especially for Windows users]. And I sincerely apologize in advance if I've
missed the instructions which clearly address these issues.


I'll present my problem as narrative based around a set of IDLE sessions.
This only very slightly contrived example, and I am not really exaggerating.

First Freshly install Python on Win32. Now download and install some modules
and packages I want to explore..

Python 2.2.2 (#37, Oct 14 2002, 17:02:34) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
IDLE 0.8 -- press F1 for help

PROBLEM #1
Where to install things on Python and how to access them?
It seems recommended default is to install modules and packages into
Lib/site-packages/some-sub-folder.

PROBLEM#2
But what about my personal work?
I can find precious little advice on that..
.
One of the first things all beginners are taught do is save a
"helloworld.py" file.
It is good practice for all software to keep user files separate from system
or application files. Better for sysadmin, maintenance and collaborative
workflow. But, in Python at present the first problem is that creating a
personal folder for scripts outside of the application installation which is
visible to import statements


>>> import jabber
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in ?
    import jabber
ImportError: No module named jabber
>>>

hmm.. go study a bit. ok it seems 'jabber' is not yet on the PYTHONPATH.

Q: How to see PYTHONPATH?

>>> PYTHONPATH
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in ?
    PYTHONPATH
NameError: name 'PYTHONPATH' is not defined
>>> $PYTHONPATH
SyntaxError: invalid syntax
>>>

nope :-((
Since PYTHONPATH  is so essential to using Python, surely it would make
sense to make that permanently available to people?
To my mind an intuitive interface would allow direct typing any of the
environment variables in idle shell.
Guess it's time to study up some more. Aha! -- sys.path

>>> sys.path
Traceback (most recent call last):
  File "<pyshell#4>", line 1, in ?
    sys.path
NameError: name 'sys' is not defined

jeez :-( study up some more

>>> import sys
>>> sys.path
['C:\\PYTHON22\\Tools\\idle', 'M:\\jasonic\\python', 'C:\\Python22',
'C:\\PYTHON22\\DLLs', 'C:\\PYTHON22\\lib', 'C:\\PYTHON22\\lib\\lib-tk',
'C:\\PYTHON22\\lib\\site-packages']

aah much better.. But still counter-intuitive imho.
If "sys.path" is so essential, then why do I have to import sys all the
time?
And why do so many texts talk about PYTHONPATH when what they really mean is
sys.path?
And how do I customize my environment so I don't have to jump through these
hoops every time?
Meanwhile my study has shown I must add jabber to sys.path. ok

>>> sys.path.append('C:\\PYTHON22\\lib\\site-packages\\jabber')
>>> import jabber
>>> jabber
<module 'jabber' from 'C:\PYTHON22\lib\site-packages\jabber\jabber.pyc'>
>>>

Yay! success :-)
Now I can follow the jabber examples from my book for a while....
I take a break and shutdown, but when I launch IDLE on my return, trouble in
paradise again:

>>> import jabber
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in ?
    import jabber
ImportError: No module named jabber
>>>

arghh.. ok guess gotta go through all that again [hope I can remember]:

>>> sys.path.append('C:\\PYTHON22\\lib\\site-packages\\jabber')
>>> import jabber
>>> jabber
<module 'jabber' from 'C:\PYTHON22\lib\site-packages\jabber\jabber.pyc'>

OK yes not such a big deal. But how to add jabber or any module permanently
to the path.?
I study this but find I am getting more confused...
Several options are suggested :

a) Run regedit and add jabber to the path entries buried there deep.
I don't like this. It feels way too far removed from python. Surely there is
some way not to have to mess with regedit.exe?
Why isn't there a simple config preferences file I can edit like just about
every other software I use?

b) edit autoexec.bat and create a PYTHONPATH entry and then add 'jabber' to
that.
Yikes this seems also too far from python. It uses a syntax I am not certain
about. I look in books and google around, but honestly it's hard to find
just some simple example I can copy. Autoexec.bat can't even use full
pathnames and its is buried like regedit until I learn I can enter
'sysedit' in the Start/Run menu as nice shortcut. Besides, I thought Python
was cross-platform and here I am messing with to much windows-specific crap,
[ahem] sorry "configuration details".

To make matters worse, I learn that apparently Python on windows uses the
registry, but IF a PYTHONPATH definition also exists in autoexec.bat, then
it will insert those values also so they show up *combined* in Python when I
call sys.path

yeow :-((
Frankly this seems totally nuts to me.
There appears to be no single common place/file where I can keep track of
path entries and edit them quickly.
Why the hell not?

It may not be in essence Python's fault, but surely ther is a better simpler
way in 2003?
Windows is still the major OS in use globally. Maybe 5-10 years later Linux
will be.

hmm... I learn from Google search and more book reading that there are some
neat ways to customize and streamline Python on my system.

c) One advises that I can create a file with .pth suffix.
Sounds good, and to be very honest I now remember that eons ago, that is how
I did successfully customize my Python installation.Alas I can';t remember
and ma having devils own time to track down the crucial steps to do this.
Unfortunately hard to find actual any examples on .pth files.
Also it too seems to depend upon successful use of PYTHONPATH and I want to
get to the heart of the matter.


d) I find some references to site.py and something promising called
"sitecustomize.py"
I try these but no luck. I feel like an idiot and am getting angry and
frustrated. I imagine what really would make sense to me and decide to post
to Edu-Sig for help.

*Dream Solution [DS]*

DS #0 -- Display PYTHONPATH when Python boots.  Make this a default, which
pros can disable via config value.

Python 2.2.2 (#37, Oct 14 2002, 17:02:34) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
IDLE 0.8 -- press F1 for help

['C:\\PYTHON22\\Tools\\idle', 'M:\\jasonic\\python', 'C:\\Python22',
'C:\\PYTHON22\\DLLs', 'C:\\PYTHON22\\lib', 'C:\\PYTHON22\\lib\\lib-tk',
'C:\\PYTHON22\\lib\\site-packages']

or  like this:

Python 2.2.2 (#37, Oct 14 2002, 17:02:34) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits", "license" or "paths" for more information.
<<<< What do you think about this ?
IDLE 0.8 -- press F1 for help


DS #1 -- To be able to enter environment variables directly like this
anytime:

>>> PYTHONPATH
['C:\\PYTHON22\\Tools\\idle', 'M:\\jasonic\\python', 'C:\\Python22',
'C:\\PYTHON22\\DLLs', 'C:\\PYTHON22\\lib', 'C:\\PYTHON22\\lib\\lib-tk',
'C:\\PYTHON22\\lib\\site-packages']

DS #2   -- To be able to add to that by setting like this:

PYTHONPATH = ['C:\\PYTHON22\\Tools\\idle', 'M:\\jasonic\\python',
'C:\\Python22', 'C:\\PYTHON22\\DLLs', 'C:\\PYTHON22\\lib',
'C:\\PYTHON22\\lib\\lib-tk', 'C:\\PYTHON22\\lib\\site-packages',
'C:\\PYTHON22\\lib\\site-packages\\jabber']  <<< the last entry is my
addition

or by adding or appending:

PYTHONPATH.add('C:\\PYTHON22\\lib\\site-packages\\jabber')
PYTHONPATH.append('C:\\PYTHON22\\lib\\site-packages\\jabber')

You'll may say "Jason, that's not the Python Way to do it. You must be
initiated."
And I reply,  "Ok, but fucking around with autoexec.bat and regedit sure
ain't the Python Way either... I've been at this for years, and still tis
nuts and that's why I am writing this long edu-sig post. Show me please!"

But I concede to learn and follow the sys.path syntax though I still feel
very strongly that there *should* also be a more direct aliasing between
environment variables outside of Python and how they are called inside
Python. Else needs to be much better explained. Just look at most books on
Python and you might se what I mean. Most are excellent but I suspect are
written by experienced programmers long familiar with handling *nix type
environment variables.

DS #3 SAVE and LOAD paths

Ok this is what I am really proposing:

sys.path.save()
sys.path.load()

These would both default to use a clear single file called "PYTHONPATH.py"
created  at installation.
My point is that people could easily set or get their own and switch on fly
like this:

sys.path.save('mypathfile.py')
sys.path.load('mypathfile.py')

where "mypathfile.py" contains a entry such as:

PYTHONPATH = ['C:\\PYTHON22\\Tools\\idle', 'M:\\jasonic\\python',
'C:\\Python22', 'C:\\PYTHON22\\DLLs', 'C:\\PYTHON22\\lib',
'C:\\PYTHON22\\lib\\lib-tk', 'C:\\PYTHON22\\lib\\site-packages',
'C:\\PYTHON22\\lib\\site-packages\\jabber']

So in a given session I might do this:

>>> sys.path.append('M:\\jasonic\\python')
>>> sys.path.save()

And I  propose the same syntax and approach should work for all the
variables in the same way across platforms.
All that remains is how best to display or set them all ? I found something
which looks close:

os.environ["ORACLE_HOME"]="/opt/app/ora/"
http://www.faqts.com/knowledge_base/view.phtml/aid/3298/fid/245

I imagine running python and seeing something this

Python 2.2.2 (#37, Oct 14 2002, 17:02:34) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits", "license", or "paths" for more information.
IDLE 0.8 -- press F1 for help

>>> paths
PYTHONPATH loaded from ''C:\\python22\PTYHONPATH.py"
PYTHONSTARTUP loaded from "M:\\jasonic\\python\\mystartup.py"
etc..

Ideally there is a single dict which holds all of these and can be easily
viewed and edited anytime on any system the same way and will always be
accessible in the Python shell. Perhaps there already is and I'm too dumb to
have found it.

*Books*
Before writing this, last night along with docs and Google research I went
to my Python shelf and pulled down the following:

- Python Essential Reference by David M. Beazley
- The Quick Python Book by Darryl Harms and Kenneth Mcdonald
- Python Programming on Win32 by Mark Hammond and Andy Robinson
- Core Python Programming by Wesley Chun
- Python Cookbook edited by Alex Martelli and David Ascher
- Python In A Nutshell by Alex Martelli

*Personal*
I realize that my main problem is I have a bad memory, especially for things
which are
counter-intuitive to me. Typically caused by switching between many
different
systems or like now when I am not daily working with it. I recognize this
as a weakness. But also on occasion it is a strength, because motivates to
find a better/simpler design.

thanks for any help
Jason
______________________________________________
Jason Cunliffe [NOMADICS: Director art+design]
Tel/fax: +1 718 422-1078 jasonic@nomadics.org
N 43:00.000' W 074:31.875' ALT:1144 ft
84 Henry Street #3C Brooklyn  NY 11201     USA
http://tranzilla.net/.v