Quick Reply
Search this Thread
Test Subject
Original Poster
#1 Old 21st Jan 2015 at 6:06 AM
Default Help!!! How Do I "compile" My Scripts to .pyo files from .py
To Anybody Who Knows Python Better than I do (not hard, Java is my native language),

After much googling I'm stuck, .

I would like to write a script mod, but my game will not recogize .py when zipped (even if placed in a sub scripts folder).

So to test my script I need to convert my .py file to a .pyo file and then zip it.

How do I do that??

(Please explain it to me like you would a 4 yr old, as I am brand new to python and I trying to do this on windows 7 when I normally use a mac for everything).

Thank you!!!!!
Pettifogging Legalist!
retired moderator
#2 Old 21st Jan 2015 at 9:07 AM
A basic way is described here:

Quote: Originally posted by SGEugi
a. Create an empty zip file in the “Mods” directory

b. Add optimized, compiled Python files to the zip file (compiled files end with the
extension .pyo). Here is a very basic way to generate .pyo files:
i. Run “python.exe -O”
ii. Import your file using "import my_file"
iii. Go into the “__pycache__” folder
iv. Rename the file “my_file.cpython-33.pyo” to “my_file.pyo”
v. Put this into your zip file

c. If you want people to be able to see what your Mod does, you can optionally put .py
(and .pyc) files into the zip file as well.


This is also in the Maxis docs I believe.

Stuff for TS2 · TS3 · TS4 | Please do not PM me with technical questions – we have Create forums for that.

In the kingdom of the blind, do as the Romans do.
Deceased
#3 Old 21st Jan 2015 at 4:03 PM
Quote: Originally posted by treelife
So to test my script I need to convert my .py file to a .pyo file and then zip it.


You may have already realized this, but not sure from your post. For initial testing of the mod, prior to packing it up into a .pyo file in a zip file for user distribution, you can put the .py source file into a "Scripts" subfolder. The Scripts subfolder needs to be in a subfolder itself, I just use a folder called "Script Testing". So in my Mods folder I have a subfolder called "Script Testing" and a subfolder in there called "Scripts". A .py source file placed in the Scripts sub-subfolder will get loaded by TS4 at startup (assuming all the syntax is valid).

The biggest benefit to this is that unoptimized scripts can be reloaded in game, so you can make changes to the script source and then just reload it on the fly for testing. There's a thread here on MTS with a reload script you can use to reload scripts in testing.
Test Subject
Original Poster
#4 Old 24th Jan 2015 at 2:41 AM
Quote: Originally posted by plasticbox
A basic way is described here:



This is also in the Maxis docs I believe.


Sorry for my delay in coming back to the thread, work unexpectedly got in the way,

Thank you, those were the first instructions I tried, but unfortunately I keep getting a error no module with that name or if I try directing it to my file through C then I get a syntax error.

I've attached a screenshot of it,
Screenshots
Test Subject
Original Poster
#5 Old 24th Jan 2015 at 2:52 AM
Quote: Originally posted by scumbumbo
You may have already realized this, but not sure from your post. For initial testing of the mod, prior to packing it up into a .pyo file in a zip file for user distribution, you can put the .py source file into a "Scripts" subfolder. The Scripts subfolder needs to be in a subfolder itself, I just use a folder called "Script Testing". So in my Mods folder I have a subfolder called "Script Testing" and a subfolder in there called "Scripts". A .py source file placed in the Scripts sub-subfolder will get loaded by TS4 at startup (assuming all the syntax is valid).

The biggest benefit to this is that unoptimized scripts can be reloaded in game, so you can make changes to the script source and then just reload it on the fly for testing. There's a thread here on MTS with a reload script you can use to reload scripts in testing.


Thank you for this I can at now start testing any code I've written.

I read about the "Scripts" subfolder in the EA mod how to guide but I didn't realize the subfolder had to be in another subfolder and was wondering why it didn't work,
Field Researcher
#6 Old 24th Jan 2015 at 9:16 AM
Python has a module zipfile that has the tools to make .pyo-files and zip-files together from py-files. Here at MTS there is a thread in modding tools showing one way to use the zipfiles-module : http://www.modthesims.info/showthread.php?t=536934
Pettifogging Legalist!
retired moderator
#7 Old 24th Jan 2015 at 11:48 AM
Thank you, clsve! I added that thread to the Tools sticky now, as well (had missed it previously).

Stuff for TS2 · TS3 · TS4 | Please do not PM me with technical questions – we have Create forums for that.

In the kingdom of the blind, do as the Romans do.
Back to top