web2py startup and sys.path

34 views
Skip to first unread message

Jonathan Lundell

unread,
Sep 20, 2010, 8:46:19 PM9/20/10
to web2py-developers
This is typical:

sys.path.insert(0, '')
path = os.path.dirname(os.path.abspath(__file__))
if not path in sys.path:
sys.path.append(path)
sys.path.append(os.path.join(path,'site-packages'))
os.chdir(path)

(fcgi alone doesn't do the chdir; bug?)

I think that site-packages will tend not to be in sys.path (except when running Rocket; that uses different logic), because Python automatically prepends the directory of the startup script to sys.path. So 'if' will never trigger, and we'll never append site-packages.

Also, it's not clear to me what sys.path.insert(0, '') is there for.

Massimo Di Pierro

unread,
Sep 21, 2010, 12:20:04 AM9/21/10
to web2py-d...@googlegroups.com
Hi Jonathan,

I will apply your patch tomorrow.

> sys.path.insert(0, '')

was added to make sure import always looks first in the folder where the importing function is located. This was necessary to avoid conflict with installed third party modules which may have the same name as web2py modules. It was a problem for simplejson for example.

Massimo

> --
> mail from:GoogleGroups "web2py-developers" mailing list
> make speech: web2py-d...@googlegroups.com
> unsubscribe: web2py-develop...@googlegroups.com
> details : http://groups.google.com/group/web2py-developers
> the project: http://code.google.com/p/web2py/
> official : http://www.web2py.com/

Jonathan Lundell

unread,
Sep 21, 2010, 1:29:10 AM9/21/10
to web2py-d...@googlegroups.com
On Sep 20, 2010, at 9:20 PM, Massimo Di Pierro wrote:
>
> Hi Jonathan,
>
> I will apply your patch tomorrow.

You ought to see if you can reproduce your ctrl-c crash first.

>
>> sys.path.insert(0, '')
>
> was added to make sure import always looks first in the folder where the importing function is located. This was necessary to avoid conflict with installed third party modules which may have the same name as web2py modules. It was a problem for simplejson for example.

I understand that part, more or less. But it ought to be redundant, since Python already inserts the directory of the starting script at the beginning of sys.path (which also means that site-packages won't be added, except with Rocket).

Also, the fcgi handler is missing the insert, as well as the chdir:

path = os.path.dirname(os.path.abspath(__file__))
if not path in sys.path:
sys.path.append(path)
sys.path.append(os.path.join(path,'site-packages'))

It seems to me that the insert "" is redundant, and the append site-packages is never happening (except for Rocket). And that fcgi is the odd man out.

Reply all
Reply to author
Forward
0 new messages