For Newbies: Pay attention when using LEO for managing a Pyhton project

17 views
Skip to first unread message

RAH

unread,
Apr 11, 2011, 9:08:15 AM4/11/11
to leo-editor
Dear All,

As I haven't yet totalled 24 hours work of LEO, it took me a while to
understand what was happening (a program working yesterday and not
anymore today ?!). My understanding is as below. If I'm wrong please
correct it.

1) directories and path for LEO:
They are driven by the @path directive of LEO and have nothing to do
with the directories or paths known to Python.

2) directories and path for Python:
They are known to Python and have nothing to do with the LEO path.

So far so good. But, where one needs to pay attention is when running
a Python script within LEO (typing CTRL-B on a node executes the
script contained in the body of that node). The Python that runs the
script is the same as the Python running LEO. It is not another
instance. This means that if you test a script A, then a script B,
then script A again, the script B might change the Python environment,
e.g. os.chdir("newdir"), so that script A won't run anymore. e.g. when
refering to a relative path (./folder/myfile). All the scripts ran
within LEO use the same instance of Python as LEO does.

One way to see what is happening is to write 2 nodes in a brand new
workbook.leo
Node A contains:
import os
print os.getcwd()
Node B contains:
import os
os.chdir("newdir")

Successively select Node A, node B, node A and each time execute the
script (CTRL-B) and you will see.
This is different from Idle where each time you execute a script (F5)
a "new shell" is reloaded.

Regards, Rene

Terry Brown

unread,
Apr 11, 2011, 9:39:27 AM4/11/11
to leo-e...@googlegroups.com
On Mon, 11 Apr 2011 06:08:15 -0700 (PDT)
RAH <rene.h...@gmail.com> wrote:

> Dear All,
>
> As I haven't yet totalled 24 hours work of LEO, it took me a while to
> understand what was happening (a program working yesterday and not
> anymore today ?!). My understanding is as below. If I'm wrong please
> correct it.
>
> 1) directories and path for LEO:
> They are driven by the @path directive of LEO and have nothing to do
> with the directories or paths known to Python.
>
> 2) directories and path for Python:
> They are known to Python and have nothing to do with the LEO path.

This thread:

http://groups.google.com/group/leo-editor/browse_thread/thread/4ea31ed2e0518866

ended with a decision to make the execution directory fixed relative to
Leo's install directory (or the .leo directory) or some such, taking
into account any @path modifications. Not sure if it actually happened
though.

Cheers -Terry

RAH

unread,
Apr 11, 2011, 2:42:43 PM4/11/11
to leo-editor
Thank you Terry to point me to this thread.
Interesting discussion.
In French, we say "Un homme averti en vaut deux"
Meaning you feel better when forwarned.
Cheers, Rene

On Apr 11, 3:39 pm, Terry Brown <terry_n_br...@yahoo.com> wrote:
> On Mon, 11 Apr 2011 06:08:15 -0700 (PDT)
>
> RAH <rene.heym...@gmail.com> wrote:
> > Dear All,
>
> > As I haven't yet totalled 24 hours work of LEO, it took me a while to
> > understand what was happening (a program working yesterday and not
> > anymore today ?!). My understanding is as below. If I'm wrong please
> > correct it.
>
> > 1) directories and path for LEO:
> > They are driven by the @path directive of LEO and have nothing to do
> > with the directories or paths known to Python.
>
> > 2) directories and path for Python:
> > They are known to Python and have nothing to do with the LEO path.
>
> This thread:
>
> http://groups.google.com/group/leo-editor/browse_thread/thread/4ea31e...

Matt Wilkie

unread,
Apr 12, 2011, 12:49:24 PM4/12/11
to leo-e...@googlegroups.com, RAH
> So far so good. But, where one needs to pay attention is when running
> a Python script within LEO (typing CTRL-B on a node executes the
> script contained in the body of that node). The Python that runs the
> script is the same as the Python running LEO. It is not another
> instance.

I have a project which needs to use python 2.5, which Leo doesn't run
on anymore. The method I use to keep using Leo with this project is to
create a button which opens a shell with the correct python
environment. It's not as quick or smooth as [Ctrl]-[B] to use Leo's
active interpreter, but it helps.

1. create a node "@button pyshell here" in the foobar-project.leo.
Body contents for that node:

-----
@
Open a windows cmd.exe prompt in the path of the currently selected node.
A mapped drive letter will be created for a UNC path.

Adapted from:
http://webpages.charter.net/edreamleo/scripting.html#working-with-directives-and-paths
http://www.mail-archive.com/leo-e...@googlegroups.com/msg12177.html
@c

d = c.scanAllDirectives(p)
# g.es(g.dictToString(d)) #uncomment to show results of above in log pane

#path = d.get('path')
path = c.getNodePath(p)

name = p.anyAtFileNodeName()
if name:
name = g.os_path_finalize_join(path,name)
g.es(name)

from subprocess import Popen
#Popen(r'cmd.exe /k pushd %s' % path)
Popen(r'%s/pyshell.bat' % path)
-----

2. Create pyshell.bat in my foobar-project directory:

-----
@echo off
echo.
echo. Setting environment for python
echo.
set PYTHONHOME=C:\Python25
set PYTHONPATH=%PYTHONHOME%;%PYTHONHOME%\Lib;%PYTHONHOME%\DLLs;C:\ESRI\ArcGIS\Bin
set PATH=%PYTHONHOME%;%path%
set py
python --version
echo.

:: add directory of the pyshell script to PATH
pushd %~dp0
set path=%path%;%~dp0

cmd /k
-----

--
-matt

Reply all
Reply to author
Forward
0 new messages