How to add uninstall script to debian package - python

I have created a simple debian package for my python program using this post.
I am also using a postinst script to setup and populate mysql tables. The package gets installed with following command.
sudo apt install mypackage.deb
I now want to add an uninstall script so that if the package is removed, uninstall script gets called to cleanup the environment.
How can I incorporate uninstall script with the debian package?

You probably need to write a postrm script too the same way as you wrote the postinst script. See maintainer scrips flowcharts to understand how these scripts work.
A quote from the same article:
"It is possible to supply scripts as part of a package which the package management system will run for you when your package is installed, upgraded or removed.
These scripts are the control information files preinst, postinst, prerm and postrm. They must be proper executable files; if they are scripts (which is recommended), they must start with the usual #! convention. They should be readable and executable by anyone, and must not be world-writable."

Related

Can users download Python code and run it with no setup?

I was curious about the process of sending someone a simple Python script, with some imported modules and how that work from the receiving end.
I guess my overall question is the following. Does the receiver of the script have to do anything at all or can they just run that file and get the intended result? Do they have to install Python, the modules used in the script, etc?
Thanks for any answers, I am sure there are plenty of “well it depends..” examples, which are fine. I am still learning so any answer is great.
If you are sending it to someone who has everything needed to develop with Python, what you need to do is work on a virtual environment:
Virtual environments (venv), in a nutshell, are python's way to handle the versioning of packages and ensuring that if someone else tries to run your script they can replicate your dependencies. To start, run
python -m venv your_venv_name
#if on linux:
source your_venv_name/bin/activate
#if on windows:
./your_venv_name/Scripts/activate
Then you will have a fresh version of the python version you were using with no dependencies installed, so you can then start installing with pip.
After you install everything run
pip freeze > requirements.txt
Now you can share your project, and the other devs just have to create their own venv and run
pip install -r requirements.txt
If on the other hand you are sending the script to someone who doesn't have python installed on their machine you will have to generate and executable file: https://realpython.com/pyinstaller-python/

Question about pip using Python from Windows Store

I have python installed through windows store and I can install programs using pip, but when I try to run said programs, they fail to execute in powershell.
How can I make sure that the necessary "scripts" folder is in my path? I never faced these problems when installing from executable.
For example, "pip install ntfy" runs successfully in Powershell.
The command "ntfy send test" fails telling me the term is not part of a cmdlet, function, etc. etc.
The 'ntfy' program is located here /mnt/c/Users/vlouvet/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0/LocalCache/local-packages/Python37/Scripts/ntfy.exe
What is the recommended way of editing my path so that programs installed via pip are available across windows store updates of the Python language?
In advance
I highly recommend you not to use python installed from the Windows Store, because you'll face such errors, and even more nasty ones.
The easy solution
Create a virtual environment on a more accessible folder, for example in C:\Users\<user>\python. To do so, do the following:
Using PowerShell, go to your user folder, using cd(Note that usually PowerShell already starts inside your user folder. This is an important setting to have, and if not, you should change your powershell starting point to this folder for the future.);
Now that you're in your user folder, type in the PowerShell mkdir python; cd python;
Now, to create a virtual environment, type python -m venv venv;
(You can verify that your virtual enviroment has been created by listing the folders, with the command ls);
You have created a virtual environment. Now, you need to activate it. To activate, run the following: ./venv/Scripts/activate;
Now, you have fully created and activated a virtual environment for your current PowerShell session. You can now install any packages / programs using pip.
After that, the only thing that you need to do is to add C:\Users\<user>\python\venv\Scripts to your Path, and you're good to go.
Caveats
By adding this folder to your Path, you may be using an outdated python version in the future, since the Scripts folder inside your virtual environment also adds a python executable that will be enabled in the path.
The recommended solution
As I stated before, I do not recommend to have the Microsoft Store version of python installed on your machine. That said, you're probably using it for the conveniences of having the latest Python version installed as soon as they're released. To alleviate this need while also getting rid of your MS Store Python. I recommend you using Chocolatey to install python (and pretty much any other programs for development).
What is Chocolatey?
Chocolatey is a package manager for Windows, pretty much like apt-get for Ubuntu Linux or HomeBrew for MacOS. By using a package manager, you get rid of the hassle of always having to execute the (mostly annoying) install wizards on windows.
To install Chocolatey:
Go to chocolatey.org/install and follow the install instructions;
(Recommended: Take a look on their documentation later to see what Chocolatey is capable of);
With Chocolatey installed, take a test drive and see if it is working properly by running choco -v in PowerShell;
By having Chocolatey installed, you can now run choco install python -y. Let's break down this command:
choco install -> The package installer of chocolatey
python -> the name of the package you want to install
-y -> This tells the installer to skip install verification by saying "Yes to All" scripts that will be executed in order to install a package.
With python installed from chocolatey, you can also see that Python is already added to your path - This means that any python package or executable installed globally will be now available on your machine!
Hope I could help you!
The above answer is good but I managed to get it to work by doing the following.
Find your installation under C:\Users\"your user"\AppData\Local\Packages it will be named something like PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0
Open your windows settings in the start menu
In search type Environment variables. Edit environment variables for your account should pop up. Click it
In the top box find Path, click it
On the right Click new and enter C:\Users\"your user"\AppData\Local\Packages\"python install directory name from 1. here"\LocalCache\local-packages\Python37\Scripts inside the little box under the last item in the list
open a new cmd prompt and type the script you wanted it should work.
On Windows you can find the user base binary directory by running
python -m site --user-site
and replacing site-packages with Scripts.
For example, this could return
C:\Users\Username\AppData\Roaming\Python36\site-packages
so you would need to set your PATH to include
C:\Users\Username\AppData\Roaming\Python36\Scripts
You can set your user PATH permanently in the Control Panel. You may need to log out for the PATH changes to take effect.

How to ensure that needed packages are always installed/available

I'm working on a script in python that relies on several different packages and libraries. When this script is transferred to another machine, the packages it needs in order to run are sometimes not present or are older versions that do not have the same functionality and cause the script to fail.
I was considering using a virtual environment, but I can't find a way to have the script use the specific environment I design as it's default, and in order to use the environment a user must manually activate it from the command line.
I've also looked into trying to check the versions of the packages installed on the machine, and if they are not sufficient then updating them from the script as described here:
Installing python module within code
Is there any easier/surefire way to make sure that the needed packages will always be available regardless of where it's run?
The normal approach is to create an installation script and have that manage your dependencies. Then when you move your project to a new environment your installer will check that all dependencies are present.
I recommend you check out setuptools: https://setuptools.readthedocs.io/en/latest/
If you don't want to install dependencies whenever you need to use your script somewhere new, then you could package your script into a Docker container.
If the problem is ensuring the required packages are available in a new environment or virtual environment, you could use pip and generate a requirements.txt and check it in version control or use a tool to do that for you, like pipenv.
If you would prefer to generate a requirements.txt by hand, you should:
Install your depencencies using pip
Type pip freeze > requirements.txt to generate a requirements.txt file
Check requirements.txt in you source management software
When you need to setup a new environment, use pip install -m requirements.txt
The solution that I've been using has been to include a custom library (folder with all of my desired packages) in the folder with my script, and I simply import them from there:
from Customlib import pkg1, pkg2,...
As long as the custom library and script stay together in the same folder, it will always have access to the right packages and the correct versions of those packages.
I'm not sure how robust this solution actually is or what possible bugs may arise from this if it is passed from machine to machine, but for now this seems to work.

Run python script from another computer without installing packages/setting up environment?

I have a Jupyter notebook script that will be used to teach others how to use python.
Instead of asking each participant to install the required packages, I would like to provide a folder with the environment ready from the start.
How can I do this?
What is the easiest way to teach python without running into technical problems with packages/environments etc.?
If you just need to install Python dependencies, you can use #Aero Blue solution. However, the users would need probably to make a virtual environment, so they don't mess with other environments and versions, etc.
However, if they should need some Linux packages, this would not be enough. Therefore, I would suggest using Docker. You would need to provide them with a Dockerfile, that you should set to install any dependencies (whether is for Python or Linux), and they would just need to use docker build and docker run commands.
The easiest way I have found to package python files is to use pyinstaller which packages your python file into an executable file.
If it's a single file I usually run pyinstaller main.py --onefile
Another option is to have a requirements file
This reduces installing all packages to one command pip install -r requirements.txt
You would need to use a program such as py2exe, pyinstaller, or cx_freeze to package each the file, the modules, and a lightweight interpreter. The result will be an executable which does not require the user to have any modules or even python installed to access it; however, because of the built-in interpreter, it can get quite large (which is why Python is not commonly used to make executables).
Have you considered using Azure notebooks or another Jupyter hosting service ? Most of these have a special syntax you can use to perform pip installs. For Azure it is !pip install
https://notebooks.azure.com

Matplotlab .exe file for Python 3.4 cannot find path to Python34

I am running Windows and am a beginner python user trying to install a few modules to run a python script. I have Python 2.7.9 and 3.4.2 both installed to the C:\ directory. I downloaded matplotlib-1.4.3.win-amd64-py3.4.exe and the corresponding .exe for python 2.7 from the Matplotlib website, but when I run the py3.4 exe the program cannot find Python 3.4 to update (Cannot install: Python version 3.4 is required, which is not found in the registry).
Python 2.7 installer works perfectly. Is there a misset PATH variable in Windows I can modify so the .exe can function properly? In CMD 'Python --version' returns Python 3.4, so unsure how to fix the issue. I installed these months ago, and may have put them in Downloads before transferring both to C:\ for clarity, which may be the problem but am unsure how to fix it.
Also, if your answer involves pip in any way please clarify how exactly to use pip in Windows. A lot of websites say to run eg. 'pip setup.py install' in the 'terminal' but do not specify if they mean Windows CMD terminal, IDLE GUI, or Python.exe command-line interface. Thanks a lot!
Not the answer to your actual question, but some clarification on your last point:
but do not specify if they mean Windows CMD terminal, IDLE GUI, or
Python.exe command-line interface.
Yes, this requires to know some context that a beginner may not have. The command pip is always used in the CMD terminal. So open CMD, and enter
pip3 install matplotlib
Notes:
Use pip3 when installing for Python 3. Then you're certain you're not accidentally installing libraries for Python 2.
pip setup.py install does not exist. You're mixing up two mechanisms to install Python packages/libraries:
One uses pip, with aforementioned pip3 install <something>. Pip goes looking online, finds a corresponding package name in a database, retrieves the URL for that package, downloads the package and installs the package. All in one command.
python3 setup.py install (again explicitly use python3 or python2 to be sure) requires you to find the package, download it, unzip it, and then in the CMD terminal, inside the unzipped folder, run the python3 setup.py install command.
This second method is usually for the latest-greatest version of a package that is not yet in pip's database, or for packages that never were in pip's database in the first place.
Generally, as a beginner, you want to stick with pip. If you ever run into the issue with the package not being available via pip, you may still be able to use pip for downloading and installing, like for example so:
pip install https://github.com/matplotlib/matplotlib/archive/master.zip
which would install the most recent matplotlib (which won't have even a version number yet, so bugs could be around).
All of these commands happen in the CMD terminal: downloading/installing packages generally all go through the terminal.
Also, when people mention "terminal", they will mean (for Windows) something like the CMD terminal. When it has to be done inside Python, it is generally called the "Python prompt". (IDLE is yet a different beast, that I'm not familiar with. I'm guessing that it has several parts, including a text editor section and a Python prompt section.)

Categories

Resources