Some Annoyances - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Some Annoyances

iwilld0itiwilld0it Posts: 1,134Member
Hey, I spent some time and setup my local iis web server to facilitate the creation of ASP pages via Python.

First off, it took me a while to integrate the concept of packages.

I find myself having to place this code at the top of my file ...

[code]
<%
import sys

# Setup Environment Path ######
path = "c:development"

try:
sys.path.index(path)

except ValueError:
sys.path.append(path)

###############################
%>
[/code]

Because A: I do not feel like setting the PYTHONPATH environment variable (because I would have to literally modify it for each web app that uses python) and B: IIS loses any information you append to the path when you restart the server process.

The other annoyance I came across involves some sort of mysterious caching. When I set up a module called ASP.py in a folder called cls, I can easily import it like so:

[code]
from cls import ASP
[/code]

Initially calling functions from this module ran fine, but say I removed a function or modify a function; the changes do not reflect. I literally have to kill the web server process, which usually means restarting iis.

I am wondering if this kind of behavior exists in python programs that are not strapped to a web server process? I can not help wondering if there is someway to invalidate a python module, so that the scripting engine must recompile it.


Comments

  • infidelinfidel Posts: 2,900Member
    : Hey, I spent some time and setup my local iis web server to facilitate the creation of ASP pages via Python.
    :
    : First off, it took me a while to integrate the concept of packages.
    :
    : I find myself having to place this code at the top of my file ...
    :
    : [code]
    : <%
    : import sys
    :
    : # Setup Environment Path ######
    : path = "c:development"
    :
    : try:
    : sys.path.index(path)
    :
    : except ValueError:
    : sys.path.append(path)
    :
    : ###############################
    : %>
    : [/code]
    :
    : Because A: I do not feel like setting the PYTHONPATH environment variable (because I would have to literally modify it for each web app that uses python) and

    Can't help you here other that to suggest some kind of .ini file with an initialization function.

    : B: IIS loses any information you append to the path when you restart the server process.

    That's because when you "import sys" you are just loading the sys module into memory. The appending of paths to sys.path is just adding another item to the in-memory list. I'm sure you knew that. Is there a way to hook the server process startup to initialize your sys.path there?

    : The other annoyance I came across involves some sort of mysterious caching. When I set up a module called ASP.py in a folder called cls, I can easily import it like so:
    :
    : [code]
    : from cls import ASP
    : [/code]
    :
    : Initially calling functions from this module ran fine, but say I removed a function or modify a function; the changes do not reflect. I literally have to kill the web server process, which usually means restarting iis.
    :
    : I am wondering if this kind of behavior exists in python programs that are not strapped to a web server process? I can not help wondering if there is someway to invalidate a python module, so that the scripting engine must recompile it.

    That's the normal way Python handles modules. The interpreter will only load a module the first time it encounters an "import" statement. The reason is to avoid recursive reference loops. If you "import A" and A does "import B" but B does "import A" then you can see the problem. You can force the interpreter to reload a module using the reload() function:

    import cls.ASP as ASP
    ASP = reload(cls.ASP)



    [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

    [code]
    $ select * from users where clue > 0
    no rows returned
    [/code]

  • iwilld0itiwilld0it Posts: 1,134Member
    : : Hey, I spent some time and setup my local iis web server to facilitate the creation of ASP pages via Python.
    : :
    : : First off, it took me a while to integrate the concept of packages.
    : :
    : : I find myself having to place this code at the top of my file ...
    : :
    : : [code]
    : : <%
    : : import sys
    : :
    : : # Setup Environment Path ######
    : : path = "c:development"
    : :
    : : try:
    : : sys.path.index(path)
    : :
    : : except ValueError:
    : : sys.path.append(path)
    : :
    : : ###############################
    : : %>
    : : [/code]
    : :
    : : Because A: I do not feel like setting the PYTHONPATH environment variable (because I would have to literally modify it for each web app that uses python) and
    :
    : Can't help you here other that to suggest some kind of .ini file with an initialization function.
    :
    : : B: IIS loses any information you append to the path when you restart the server process.
    :
    : That's because when you "import sys" you are just loading the sys module into memory. The appending of paths to sys.path is just adding another item to the in-memory list. I'm sure you knew that. Is there a way to hook the server process startup to initialize your sys.path there?
    :
    : : The other annoyance I came across involves some sort of mysterious caching. When I set up a module called ASP.py in a folder called cls, I can easily import it like so:
    : :
    : : [code]
    : : from cls import ASP
    : : [/code]
    : :
    : : Initially calling functions from this module ran fine, but say I removed a function or modify a function; the changes do not reflect. I literally have to kill the web server process, which usually means restarting iis.
    : :
    : : I am wondering if this kind of behavior exists in python programs that are not strapped to a web server process? I can not help wondering if there is someway to invalidate a python module, so that the scripting engine must recompile it.
    :
    : That's the normal way Python handles modules. The interpreter will only load a module the first time it encounters an "import" statement. The reason is to avoid recursive reference loops. If you "import A" and A does "import B" but B does "import A" then you can see the problem. You can force the interpreter to reload a module using the reload() function:
    :
    : import cls.ASP as ASP
    : ASP = reload(cls.ASP)
    :
    :
    :
    : [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]
    :
    : [code]
    : $ select * from users where clue > 0
    : no rows returned
    : [/code]
    :
    :
    -You Wrote-
    "Is there a way to hook the server process startup to initialize your sys.path there?"

    Well there is a file in all asp projects called global.asa. There is an Application_Start event in this file (which fires at the beginning of the web process creation), which I suppose could house the code. I'll have to see if python can hook into it. The solution I used so far, so that I am not rewritting the code, is to use an include statement ...

    [code]


    <%
    # Other Python Code Here
    %>
    [/code]


Sign In or Register to comment.