Syncing .ipynb and .py files? - python

I created all of my python files (for daily file processing) in Jupyter because it makes it easy for me to view what is going on with the data. Once I finalize something, I create a .py file and then I have Windows Task Scheduler run it for me.
At some point I started to edit these .py files in Pycharm here and there, minor changes or tweaks. Now my original .ipynb files are a bit useless and out of date.
Is there anyway to keep these files synced? Or just a best practice that I should file? Right now I don't really know what is changed, so I am literally going to copy the .py files and paste them into the .ipynb files and overwrite it.

Here is the answer to this question in another tread:
Synchronizing code between jupyter/iPython notebook script and class methods
To cut it short you need to use reload function from importlib module:
import babs_visualizations
from importlib import reload
reload(babs_visualizations)
Just a little addition: make sure that you are addressing the function in the form of moldule.function. If you previously imported function by from module import funciton and then reloaded the module the function will not be reloaded!
Cheers.

Related

how to simulate a symlink from a .py file to a .pyw file?

I want to have 2 copies of my python script, one as .py extension and the other as .pyw extension (Windows version, no console)
That allows me to run the .py file and get output from console (from command line) and associate the .pyw file to graphical programs.
Of course, the worst option is to duplicate files, which works but maintainability suffers.
When I was using clearcase, I used to create a symlink from one file to another but symlinks aren't the best choice when managing a code base.
I could create a helper module that I import in both cases to share the maximum of source code, but I have a lot of scripts in that case, and I'd like a generic solution.
What's the alternative to copying the whole file?
Here's a generic solution that doesn't depend on the contents of the .py file.
Say I have a foo.py file and I want to create a foo.pwy with the exact same behaviour, let's create this foo.pyw file with this exact contents in the same directory:
import os
with open(os.path.splitext(__file__)[0]+".py") as f:
contents = f.read()
exec(contents)
As one can see, the name foo is nowhere to be seen in the contents. The script takes the name of the script, changes the extension to .py, reads it and executes it.
With some simple scripting, one can easily replace all copies of .pyw files to reference the .py files with this exact content no matter the contents of the original .py file.
This saved a lot of hassle when we migrated from Clearcase to git and had to drop the symbolic links.

Why can python files be modified while open in IDLE editor?

When a .py file is being edited in IDLE, it can be renamed or deleted while still being able to run, but in other file types such as word files this is not allowed. You can even create a script using the os module to delete itself and then do an action, and that action still works. How is this possible?
When you run python scripts you scripts were loaded into memory. So you can edit your script files.

how to persist/store data in executable compiled with pyinstaller

I need to locally store some basic data the user inputs into my exe. The program was compiled using pyinstaller and was previously making use of the os open method of saving data to txt files. It is my understanding that macOS(my OS although cross-compatibility with Windows would be great) locks executables so that they may not make any file changes. The program currently does save the data, but when the executable is run again the old data is no longer accessible.
It seems obvious that apps store things locally all the time, how can data be persisted specifically within the python/pyinstaller combination?
Apologies if this is a simple question, it definitely seems simple but I can't find documentation for this anywhere.
You can use
os.path.expanduser('~user')
to get the user home directory in a cross-platform manner, see How to find the real user home directory using python?
Your application should have write permissions in the user home directory, so then you can let it create and modify a data file there following How to store Python application data.

Importing a python module built in Tkinter (which is reliant on images) from a different directory

I am trying to import a python program from within another python program, however these are not in the same directories, and so I am using this code:
import sys
sys.path.append("C:/Users/Name/Desktop/Project")
import Maths
This works, and it opens the program. However, the problem I am having with this is that the program I am importing relies on some images and files that don't seem to get loaded (which prevents the program from running properly). These files are placed in
C:/Users/Name/Desktop/Project/resources
What I've Tried
I have tried placing my program that I am importing into the same directory as the files it relies on, but this came with the same error as shown here:
couldn't open "resources/bg.png": no such file or directory
So my question is - how can I fix the issue I describe above?
One possible solution is to turn Maths module into an import package. Put it inside your working folder, in a subfolder. Put an empty __init__.py in that subfolder to make Maths a package. You can now import it as usual from any module that is started from the working folder. To solve the error you'll have to abstract the resources location inside the Maths code. Try using pkgutil.get_data(package, resource) to get the contents of those files.
See pkgutil docs, in the page bottom.

How to retain an "import" capability after py2exe compile

I wrote a wxPython GUI where I currently configure some of the widgets and some default values by using "import data" for a module file containing several lists.
But I need to compile the whole program using py2exe for a user without a python installation.
In so doing, I lose the capability of letting the user edit the data.py file to change
those configuration defaults.
I could put each list as a series of text strings and read and parse the whole thing, but that
seems like a huge waste when python already can do all that by a simple import statement.
I could probably do it with xrc/xml or perhaps ConfigParser but it would seem there should be an easy way to sort of
import data.txt
or something similar and let python do it's thing! Then when py2exe gets hold of it it, it wouldn't create un-editable byte-code for the data.txt file.
Any suggestions?
files that are imported are bundled in the executable by py2exe. The way to go is to use a configuration file that you package with your executable in a zip or with Inno Setup. Configuration files are files made to be changed at some moment, contrarily a user should not be modifying a python script. I tell you because some 'negative' experiences to say something polite.
For my programs (practically all use wxPython GUIs) I use to have a py module with configuration data (directories, etc) and some globals. This module is used to load default parameters if the program does not find my .ini file or if that especific parameter has not been set in the ini. Then I distribute everything (exe and auxiliary files) with inno setup.

Categories

Resources