user1758952
user1758952

Reputation: 487

How to use a specific version of python interpreter to run a script without installing it on a system?

My code can only run on specific version of python (64 bit python 3) and with some libraries installed. However, I don't want to install the specific version of python and the libraries in every computer I want to run it on. Is there anyway to run it without any installation of that specific version of python and libraries?

Upvotes: 0

Views: 210

Answers (2)

bfris
bfris

Reputation: 5805

A typical way to accomplish this is to use PyInstaller which compiles your Python into bytecode and makes an executable file. It also gathers up all of your dependencies and stuffs them into a folder for you. Py2Exe and Nuitka do similar things. Nuitka can make your code run faster in some cases if that's important to you.

All of these solutions will transform your source .py files into something else. If you wanted to distribute the source files AND your specific Python distribution, I don't know that there is a specific project for that use case.

I use PyInstaller to deploy my application to other computers in my organization. When I add new features to my application, I will often rebuild the distribution folder, but only copy the generated executable file to client computers. That model works fine if all of the different libraries and modules haven't changed. If you change libraries in your application or move to a new version of Python, then you'll want to copy the whole distribution folder to clients.

Upvotes: 0

Janekx
Janekx

Reputation: 641

I list my ideas about your problem. I personally used them all in the past and they all worked. You can use either way but before use any of them, check the pros and cons, and choose wisely according to your problem (e.g. if you just want to try out a script in Python 2.7, (2) and (3) is the perfect solution, but if you develop both in 2.7 and 3.8, (4) or (1) may be the best. If you want your customers to be able to run your application with ease, you may choose (1))

(1) You can check Docker. There are specific Docker images for specific Python versions. Check Docker site of Python. You can select specific versions on which your code will run. If you need packages to be installed, you can run your requirements.txt to install all of them, but if you need another application to be installed, you can create your own Docker image based on one of the listed images.

(2) Another, more pythonic way is to use virtualenv and specify which Python you wish to use. However, this also requires to download and unzip another version of Python, but is not required to be installed.

The corresponding flag in virtualenv:

discovery:
  discover and provide a target interpreter

  --discovery {builtin}         interpreter discovery method (default: builtin)
  -p py, --python py            target interpreter for which to create a virtual (either absolute path or identifier string) (default: c:\python38-32\python.exe)

(3) Also, if you unzip another version of Python, you can simply use that with using the absolute path of the executable.

(4) And my last idea is to use pyenv which is a Python version manager. It is easy to use and switch between Python versions, I used myself that in the past. Check their website too.

(5) You can pack the interpreter and the application into a single executable with py2exe for example. I did not use this tool ever.

Upvotes: 1

Related Questions