+ Reply to Thread
Results 1 to 7 of 7

Thread: startup.py and scripting.py Doubts

  1. #1
    André Cardoso
    Join Date
    Oct 2011
    Posts
    69
    Points
    2
    Answers Provided
    0


    0

    Default startup.py and scripting.py Doubts

    Hello

    I have two questions regarding the startup.py and the scripting.py:

    1: As described in the docs, the startup.py common use is to load custom scripts on CE load (sys.path.append(), import); What is the common use case for the scripting.py ? What should I not do in either one?

    2: I have created a startup.py on the workspace root (actually, it is a symbolic link) and I put some debugging "prints" to see if it worked. Upon CE loading, I see those messages written on the Console. However, when I open the Jython console, and try to access the modules imported inside the startup.py, it does not work. I'm forced to rewrite the code inside the startup.py file in order to access the desired functions... After reading the docs about the startup.py script, I got the impression this was one of its purposes. Can you help me here? Am I doing something wrong?

    Thanks!
    André
    Last edited by thyandrecardoso; 03-05-2012 at 03:01 PM. Reason: Grammar

  2. #2
    Andreas Ulmer
    Join Date
    Nov 2011
    Posts
    52
    Points
    22
    Answers Provided
    4


    0

    Default Re: startup.py and scripting.py Doubts

    Hi André

    startup and scripting are appended to the regular python startup script in the following way:
    1. file startup.py is copied and loaded as a module
      Code:
      import startup
    2. content of scripting.py is appended as text

    resulting in:
    Code:
    ...
    #loading regular api modules
    from jscripting.CE import *
    ...
    
    # user startup
    import startup
    
    # scripting
    <TEXT IN SCRIPTING.PY>
    (see scripting.py in directory $USER/.CityEngine/@CEVERSION/jythonCache)


    functions in startup need to be called with their module name, e.g.
    Code:
    startup.myCustomFunction()

    two words of warning:
    1. Due to the mechanisms described above, typos/errors in both startup.py or scripting.py can break the whole Python scripting interface.
    2. Both startup.py and scripting.py are loaded ONLY on CityEngine start. When changing these files, make sure to restart CE.


    Hope that helps
    Andreas
    Last edited by andreas.ulmer; 03-06-2012 at 12:50 AM.

  3. #3
    André Cardoso
    Join Date
    Oct 2011
    Posts
    69
    Points
    2
    Answers Provided
    0


    0

    Thumbs up Re: startup.py and scripting.py Doubts

    Thank you Andreas!
    I found out my problem eventually. After looking at scripting.py in the cache directory, I found out that the scripting did not have what it should (neither the import startup, or the contents of scripting). So I deleted the file. Restarted CE. Same behaviour :S

    Then I deleted the "class" file associated with the python file, "scripting$py.class" in the jython cache dir. Restarted CE, and all was OK . I tried to open the scripting.py file again, and now I saw the "import startup" line

    So, despite multiple restarts between each edit of the files in the workspace root, the jython cache failed to refresh...

    Once again, thanks for the tips!
    André


    Quote Originally Posted by andreas.ulmer View Post
    Hi André

    startup and scripting are appended to the regular python startup script in the following way:
    1. file startup.py is copied and loaded as a module
      Code:
      import startup
    2. content of scripting.py is appended as text

    resulting in:
    Code:
    ...
    #loading regular api modules
    from jscripting.CE import *
    ...
    
    # user startup
    import startup
    
    # scripting
    <TEXT IN SCRIPTING.PY>
    (see scripting.py in directory $USER/.CityEngine/@CEVERSION/jythonCache)


    functions in startup need to be called with their module name, e.g.
    Code:
    startup.myCustomFunction()

    two words of warning:
    1. Due to the mechanisms described above, typos/errors in both startup.py or scripting.py can break the whole Python scripting interface.
    2. Both startup.py and scripting.py are loaded ONLY on CityEngine start. When changing these files, make sure to restart CE.


    Hope that helps
    Andreas

  4. #4
    André Cardoso
    Join Date
    Oct 2011
    Posts
    69
    Points
    2
    Answers Provided
    0


    0

    Default Re: startup.py and scripting.py Doubts

    Sorry, but I want to resuscitate this thread :P

    The first thing:
    - in linux (don't know what's the behaviour on windows) when I delete the startup.py file and restart CE, it continues to behave as if the file was there. When I inspect the cache, it seems the both the *.py file and its *.class file continues there, and CE disregards that the file was deleted on the workspace. I was forced to remove both files from the jython cache.

    Second:
    - the most annoying thing is that, in spite of my startup file being read by CE, when I open a python Console on CE, I cannot access the modules that were imported there. Actually, all the debug messages I put on the startup.py get printed when the console opens, but after that I cannot access the module functions (either directly or using the module name).

    I did not pursue the problem further, because I've been out of time... but when I have more details I'll post them here.

    Can someone test this behaviour on linux?

    Thanks!!
    André Cardoso
    Developer(3Decide)
    URL: www.3decide.com

    E-mail: andre.cardoso@3decide.com
    Address: PINC UPTEC – Praça Coronel Pacheco, 2,
    4050-453 Porto, Portugal

    http://about.me/andrecardoso

  5. #5
    André Cardoso
    Join Date
    Oct 2011
    Posts
    69
    Points
    2
    Answers Provided
    0


    0

    Thumbs up Re: startup.py and scripting.py Doubts -- solved

    Ok, I think I found out what was going on and it was my fault :S

    On the matter of my imports not working:
    - startup.py makes CE do a "import startup"; if, inside your custom "startup.py", you do a "import fantastic_module", then on the python console you'll need to do something like:
    Code:
    >>> startup.fantastic_module.foo()
    to call your functions.
    And my problem was that I simply was not starting my call with "startup.", instead I was trying to access the modules directly.... My fault!
    I think Andreas Ulmer tried to explain this to me on the first post but I did not quite get at the time!



    - on the other hand, if you create a "scripting.py" inside the workspace root, their contents are copied to the CE own initialization script; So, if inside "scripting.py" you do a "import fantastic_module", then on the console you'll need to something like:
    Code:
    >>> fantastic_module.foo()

    So, sorry for the entropy caused here :S


    ****

    Nevertheless, I was able replicate the behaviour concerning the cache, on linux:
    - when I delete the startup or scripting file from the workspace, they continue to exist in cache. I'm forced to delete the "scripting.py", "startup.py" and associated *.class files to reset the scripting environments.
    André Cardoso
    Developer(3Decide)
    URL: www.3decide.com

    E-mail: andre.cardoso@3decide.com
    Address: PINC UPTEC – Praça Coronel Pacheco, 2,
    4050-453 Porto, Portugal

    http://about.me/andrecardoso

  6. #6
    Robert Hexter
    Join Date
    Mar 2012
    Posts
    78
    Points
    5
    Answers Provided
    1


    0

    Default Re: startup.py and scripting.py Doubts

    "I'm forced to delete the "scripting.py", "startup.py" and associated *.class files to reset the scripting environments."


    Can confirm that you have to manually remove these from the Cache and restart in order to use this system as this is not done automatically when the files change.

    I am doing this on windows7x64.

  7. #7
    Andreas Ulmer
    Join Date
    Nov 2011
    Posts
    52
    Points
    22
    Answers Provided
    4


    0

    Default Re: startup.py and scripting.py Doubts

    I did a bit of testing and found the following behaviour:
    1. scripting.py is not updated on CityEngine restart. Happens on all platforms. To trigger update, scripting.py and scripting$py.class need to be manually deleted from JythonCache. clearly a bug in CE 2011.2 (probably earlier versions as well)
    2. startup.py is reliably updated on CE startup.

    I therefore recommend not to use a scripting.py file, but work with startup.py only instead.

+ Reply to Thread

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts