How do I deploy a python script that has prerequisites - python

I'm developing an application that requires some initial configuration when first deploying(initial params, server adress,...etc). This application is written in a different language than python, but I'm using python because it's the language that's the most commonly pre-installed on linux machines.
I'm thinking of installing the prerequisites at the top of the script like so:
import os
os.system("python setup.py install <package>")
from <package> import <stuff>
But then I'm installing a package on a computer that belongs to a user, only to use it just once. Should I just uninstall it when my script ends? How do I go about this?

Why not install the python packages from your shell script prior to execution of your program? See this question if you're trying to uninstall these packages afterwards. It looks like if you install with easy_install or pip, you can just use pip uninstall.
Update based on comments:
You can also consider deploying your script as a separate application using cx_freeze, py2exe, or some other option (see additional info here: http://docs.python-guide.org/en/latest/shipping/freezing/).

Related

Remote interpreter deployment for python script

I'm planning to run a machine learning script consistently on one of my Google Cloud VMs.
When I configured the remote interpreter, unfortunately all the imported libraries where not recognized anymore (as they might not be installed in the virtual envoirement in the cloud). I tried to install the missing modules (for example yfinance) through the Pycharm terminal extension within my remote host connection over SSH and SFTP. So I basically chose the 188.283.xxx.xxx #username in the Pycharm terminal, and used pip3 install to install the missing modules. Unfortnately my server (due to limited ressources) collapses during the build process.
Is there a way to automatically install the needed libraries when connecting the script to the remote interpreter?
Shouldn't that be the standard procedure? And if not: does my approach make sense?
Thank you all in advance
Peter
You could use something like importlib to install your modules at runtime, but I'd just opt for creating a requirements file using pip freeze > requirements.txt which you can then use on the server to get all your dependencies in one go (pip install -r requirements.txt) before your first run.
If it fails for any reason (or when you've updated the requirements file) you can run it again and it will only install whatever wasn't installed before.
This way it's clear what modules (and which version of each module) you've installed. In my experience with machine learning using the right version or combination of versions can be important, so it makes sense to define those and not just always get the latest version. This especially helps when trying to run some older project.

How to create a package to install all the dependencies python script needs

My project consists of a python script(.py file) which has following dependencies :
1) numpy
2) scipy
3) sklearn
4) opencv (cv2)
5) dlib
6) torch
and many more ...
That is , the python script imports all of the above.
In order to run this script I need to manually install all of the dependencies by running 'pip install' or 'sudo apt-get install' commands on bash.
For dependencies like dlib , opencv and torch I need to curl the respective repositories build them using cmake and then install .(Here again i need to apt-get install cmake).
As a result I run a lot of commands just get the setup ready to run one python .py script.
Is there anyway I can build all these dependencies , package them , and just install everything using one command ?
PS :- I am a beginner in python . So please forgive if my question seems silly .
Thanks !!
Manasi
I know that this Response may be a bit late. However, even if you can't benefit from this information now, perhaps someone else who may be looking for a similar answer will stumble onto this posting one day.
You can use py2exe or pyinstaller Modules, along w/ the conda Package Manager to Package and Compile an Executable. You will also need to install pywin32, if you're working on the Windows Platform.
If your project includes Non-Python Dependencies, you may also want to take a look at NSIS (Nullsoft Scriptable Install System). If you plan on running Python Scripts during the Unpacking/Installation process, the NSIS Website also has NsPython Plugins available, for that purpose.
I hope this helps to get you started!
In case of only python dependencies, use virtualenv.In case of others, write a shell script which has all the installation commands.

Specifying installation path of python modules from installed RPM

I have a simple python script that I have been trying to package into an RPM. This was a simple python script that you can run by calling "./". However, to package it into an RPM, I turned the script into a module with a init.py and setup.py. I was able to package it into an rpm using "python setup.py bdist_rpm" following https://docs.python.org/2.0/dist/creating-rpms.html . I was also able to install the created .noarch.rpm file into a different machine. However, I have no idea how to use this script now after I installed the .noarch.rpm file.
So, I successfully installed the .noarch.rpm file that has my script packaged into it, but I have no idea where this file is or how to use my script from this. This is my very first time creating an RPM, and I am fairly new to Python as well, so I think I am just missing something. Is there a way to specify where the python module is installed when I install the .noarch.rpm?
I am running on RHEL. I also looked at two other StackExchange questions/answers that are similar to what I want, but I still do not quite understand what to do. Here are the other questions/answers: Python distutils - Change Path RPM Installs To and Creating Python RPM
You can get list of files in your RPM package by:
rpm -qpl ./some_package.rpm
or when already installed:
rpm -ql some_package

Create standalone applications with py2app by not using the system installation of Python on Mac?

I am using py2app to package a Python application to be used on other Mac computers. I am currently running OSX 10.7.5 and the system Python installation on my computer is Python 2.7.1. When I package the program with py2app, it works on my computer, but will not work on another computer - the error that comes up is it cannot locate a Python runtime.
From what I have read about this, it looks like my py2app build is using the system installation of Python on my computer and therefore will only create a semi-standalone application instead of a standalone application.
Also, I have seen that to fix this you need to package it with a separately downloaded Python. I have downloaded a separate Python and even tried to change my PYTHONPATH in my .bash_profile file, but cannot seem to get py2app to build with a different version of Python.
Can anyone point me in the right direction as to how to do this?
I have read other questions and wasn't able to find out how to do it in my case. If there is any other information you need to know to help, please let me know.
py2app builds the application bundle using the running version of python. To use the separate install of python you therefore have to make sure that py2app and the other libraries you use are available in that installation of Python, then use that installation to build the application.
For example:
$ /Library/Frameworks/Python.framework/Versions/2.7/bin/easy_install py2app
$ .../bin/easy_install ...
$ /Library/Frameworks/Python.framework/Versions/2.7/bin/python setup.py py2app
The simplest way of handling this IMO is by utilizing MacPorts. You can download and install a standalone version of Python and just about any other package you might need.
Get macports: https://www.macports.org
sudo port install py27-py2app
sudo port select python python27
Now your standalone Python is the default, and py2app will run and bundle using that version of Python.

Create Python Script RPM for CentOS

I installed a minimal version of CentOS 6.3 on a virtual machine. I then used
yum install python to install python-2.6.6-29.e16_3.3.x86_64 to /usr/bin/. I have a custom python script that I wrote and I want to create an RPM of it including all python dependencies so that a centOS user can use rpm command to install my python script and all needed python libraries. Is this a common practice? It seems like the below command is the recommend approach, but I don't have a setup.py file. Any ideas?
python setup.py bdist_rpm
Are you sure you need RMP file to be used for installation? After querying for RMP in Google Search, it gave me RMP is a "RealPlayer Metadata Package File". Which I am sure you are not looking for. However based on your other statements I can make out you are trying to create a rpm file which you want to distribute with all the dependencies, And for that you are using the command python setup.py bdist_rpm, This is usually the correct approach but the per-requisite is you need to create the setup.py. Refer to this link http://docs.python.org/release/2.0/dist/setup-script.html
Which talks about how to create your own setup.py file.

Categories