Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

Cleanup Python cache files (*.pyc)

marcinszydlowski.1984
Enthusiast

Cleanup Python cache files (*.pyc)

marcinszydlowski.1984
Enthusiast
Enthusiast

Hello,

 

My plugins are splitted into main script and dependent modules which contains commonly used code. The problem is that this prevents the plugin to be fully uninstalled because there are still __pycache__/*.pyc files inside its directory.

 

The first attempt was to turn off python cache at the beginning of the script:

import sys
sys.dont_write_bytecode=True
...

and turn it on again after process finish. Generally, it works on Windows 8.1 (I haven't tested on Mac) but the problem may be in a situation when user runs some other Python processes on his machine which needs *.pyc files while my plugin will try turn them off.

Another disadvantage of this way is that all scripts of the plugin must be recompiled everytime when the user runs the plugin.

Changing prefix for __pycache__ is not acceptable because it'll change global path on user's machine.

 

I also checked stackoverflow forum but the best suggestion from there is to make one big file by writing own consolidator or use mergers which have very specific logic. There is also something like Python Eggs or Wheel but I don't know it is supported by the Fusion.

 

Currently, the only option for me is writing modules as classes which let me to simply copy theirs code to main script before I sent package to the review.

 

Is there any possibility to remove *.pyc files after uninstalling plugin?

0 Likes
Reply
Accepted solutions (1)
942 Views
4 Replies
Replies (4)

OceanHydroAU
Collaborator
Collaborator

How are you uninstalling?

 

One idea to think about perhaps - tell it to put the cache files in a temp folder instead?

https://stackoverflow.com/questions/471928/way-to-have-compiled-python-files-in-a-separate-folder

0 Likes

marcinszydlowski.1984
Enthusiast
Enthusiast

Thank you @OceanHydroAU . I'm using standard installer prepared by the store team and I don't want to changing it. I read previously the post you sent. However, setting mentioned there changes environment variable. It's good when I am sure that it won't affect other Python processes but I cannot do this on client's machine.

I think I'll stay with one file. It takes some time but seems to be the best option for now.

1 Like

BrianEkins
Mentor
Mentor
Accepted solution

Another option is to deliver the .pyc files so they're included in the installer and will be uninstalled.  The only downside to this that I'm aware of is that when/if Fusion updates to a new version of Python, you'll need to recompile your application because the .pyc files are tied to a specific version of Python.  However, this has only happened a couple of times in the lifetime of Fusion so far.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
0 Likes

marcinszydlowski.1984
Enthusiast
Enthusiast

Yes, the last installer has been prepared in that way but as you mentioned it is vulnerable to compiler changes.

I will prepare one file in my plugins but also include dummy *.pyc files to be sure that previous versions will be overwritten and uninstalled during plugin's removal. Thank you both @OceanHydroAU  and @BrianEkins for suggestions.

0 Likes