0

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.

  • 2
    In order to run a Python script the user needs to have Python installed, plus any external modules that don't ship with your program. – MattDMo Oct 19 '22 at 01:52
  • 1
    https://stackoverflow.com/questions/9960583/how-do-you-make-an-installer-for-your-python-program might be worth looking at – CollinD Oct 19 '22 at 01:55
  • 1
    Just to inform you, many email providers will block sending `.py` files. For example, instead of sending it with gmail you would have to upload it to drive and share a link. With regard to running the python file with minimal dependencies, as long as the user has python on their system, you can use the `subprocess` module to run pip and install packages. The user may need to manage their firewall and antivirus settings. All in all, **you should probably use an online service like google collab or jetbrains datalore to share and execute python scripts.** – Michael Sohnen Oct 19 '22 at 01:59
  • 1
    This is a tricky question the most direct way of doing this is using pyinstaller or any py to .exe convert because it comes with a build – Flow Oct 19 '22 at 02:04

1 Answers1

2

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/

Rami16
  • 127
  • 9
  • 1
    Awesome thank you. I was looking at venv as a didn’t really understand it, so this clears up a few things. –  Oct 19 '22 at 03:15