Windows installer does not clean up *.pyc and *.pyo files on uninstall - python

I have a python app (PyQt4 app) for which I create a windows MSI installer using setuptools.
python setup.py bdist_msi
The MSI installs the application correctly (under site-packages). On uninstalling (msiexec /x) it also removes all the associated *.py files. However, the compiled files still persist on disk. Subsequently when I run the new version of the app, it still picks up stale *.pyc files.
Is there an option to somehow tell the MSI to clean up *.pyc and *.pyo files?
(I suspect this is because *.pyc and *.pyo files were not installed by the MSI in the first place, but rather created upon running python.exe. But would appreciate some guidance).
Thanks

You are absolutely correct. Msi do not remove those file because they were not deployed by installer. In order to clean them up you have to create custom-action which will delete all *.pyc and *.pyo files.
Just in case you know exact filenames you could add dummy (empty) files so that installer would consider them it's "property" and delete them on uninstall.

Related

Can .conda_trash files be safely deleted from Scripts folder in Anaconda?

I am using Anaconda python distributin. Under Scripts folder, I see several ~.conda_trash files. Can these files be safely deleted?
I am using Windows 10, anaconda 2020_07.
The .conda_trash file are generated on windows when conda tries to delete folder containing in-use files. As windows can't delete files that are in use (i think linux users don't meet the .conda_trash problem).
There is a delete_trash function at boot that scans the entire tree in search for those files and deletes them.
So basically conda should be able to get rid of those files by itself. But if those are not needed anymore (and take too much time at boot), it shouldn't be a poblem to manually delete them.
I have tested on my PC that the ~.conda_trash files can be deleted from Scripts folder without affecting anaconda distribution.

clean up .pyc files in virtualenv stored in souce repository after the fact?

I've created a virtualenv for my project and checked it into source control. I've installed a few projects into the virtualenv with pip: django, south, and pymysql. After the fact I realized that I had not set up source control for ignoring .pyc files. Could there be any subtle problems in simply removing all .pyc files from my project's repository and then putting in place the appropriate file ignore rules? Or is removing a .pyc file always a safe thing to do?
That is fine, just remove them!
Python auto-generates them from the corresponding .py file any time it wants to, so you needn't worry about simply deleting them all from your repository.
A couple of related tips - if you don't want them generated at all on your local dev machine, set the environment variable PYTHONDONTWRITEBYTECODE=1. Python 3.2 fixed the annoyance of source folders cluttered with .pyc files with a new __pycache__ subfolder

Keeping the .dll and .pyd files in other directory

I am using cx_Freeze to convert Python scripts to Windows executable. I am using cxfreeze script present in the Scripts directory. I want the executable generated by cxfreeze to be in a different directory and the .dll's and .pyd's in a different one. When I tried to put the two of them in separate directories the .exe did not work I got
The application has failed to start because python33.dll was not found. Try reinstalling to fix this problem
I know this happends because the executable and (dll's & .pyd's) are located in different directories. Is there a way to keep them in different directories ?
I am using the following command to generate the executable
C:\Python33\Scripts>cxfreeze C:\Users\me\Desktop\setup.py --target-name=setup.exe --target-dir=C:\Users\me\Desktop\new_dir
Python for Windows really requires that the main pythonXX.dll (in this case, python33.dll) exists in C:\windows\system32\
In all of our various combinations of installing Python to different locations, network drives, etc. we've always had to use a little batch file to copy pythonXX.dll into the system32 dir.
I don't think PATH manipulation will work for you, just try copying the dll to system32 and see if your issues go away.
Then again, if you installed another version of Python to say C:\Python33 , then that windows-based installer will do this for you, and you'll be able to run your other Python location.

How can I make portable python desktop application?

The requirement is to make an application portable, meaning no installer. I looked at py2exe and I am afraid I need to run install if I want to run it under Windows.
So my question is, can I make a portable python desktop application without any installation (all dependencies and libs are packaged), dragging from USB / CD will run it?
(This is critical because it's a headache for users to install C++ Run Time library...)
Thanks.
You can use this method with py2exe: http://www.py2exe.org/index.cgi/SingleFileExecutable
Basically, you use NSIS to package all of the required files and folders into a single executable. When you run it, the required files are expanded to a temporary directory, the executable is run, and when it exits, the temporary files are deleted automatically.
There is also an example that comes with py2exe which uses Inno Setup instead of NSIS to achieve the same result. It's installed to site-packages\py2exe\samples\extending.
You can also fork Portable Python and modify to include your application and libraries you need. It runs from any drive/network location without installation and you can pick do you want 2.x.x or 3.x.x based Python core

How do I setup python to always include my directory of utility files

I have been programming in Python for a while now, and have created some utilities that I use a lot. Whenever I start a new project, I start writing, and as I need these utilities I copy them from where ever I think the latest version of the particular utility is. I have enough projects now that I am losing track of where the latest version is. And, I will upgrade one of these scripts to fix a problem in a specific situation, and then wish it had propagated back to all of the other projects that use that script.
I am thinking the best way to solve this problem is to create a directory in the site-packages directory, and put all of my utility modules in there. And then add this directory to the sys.path directory list.
Is this the best way to solve this problem?
How do modify my installation of Python so that this directory is always added to sys.path, and I don't have to explicitly modify sys.path at the beginning of each module that needs to use these utilities?
I'm using Python 2.5 on Windows XP, and Wing IDE.
The site-packages directory within the Python lib directory should always be added to sys.path, so you shouldn't need to modify anything to take care of that. That's actually just what I'd recommend, that you make yourself a Python package within that directory and put your code in there.
Actually, something you might consider is packaging up your utilities using distutils. All that entails is basically creating a setup.py file in the root of the folder tree where you keep your utility code. The distutils documentation that I just linked to describes what should go in setup.py. Then, from within that directory, run
python setup.py install
to install your utility code into the system site-packages directory, creating the necessary folder structure automatically. Or you can use
python setup.py install --user
to install it into a site-packages folder in your own user account.
Add your directory to the PYTHONPATH environment variable. For windows, see these directions.
If it's not in site-packages then you can add a file with the extension .pth to your site-packages directory.
The file should have one path per line, that you want included in sys.path

Categories

Resources