startup.py and scripting.py Doubts

2229
6
03-05-2012 09:21 AM
AndréCardoso
New Contributor III
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é
Tags (3)
0 Kudos
6 Replies
AndreasUlmer
Occasional Contributor
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

  2. import startup

  3. content of scripting.py is appended as text


resulting in:

...
#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.
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
0 Kudos
AndréCardoso
New Contributor III
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é


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

  2. import startup

  3. content of scripting.py is appended as text


resulting in:

...
#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.
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
0 Kudos
AndréCardoso
New Contributor III
Sorry, but I want to resuscitate this thread 😛

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!!
0 Kudos
AndréCardoso
New Contributor III
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:

>>> 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:

>>> 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.
0 Kudos
RobertHexter
New Contributor III
"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.
0 Kudos
AndreasUlmer
Occasional Contributor
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.
0 Kudos