Can users download Python code and run it with no setup?
Question:
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.
Answers:
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/
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/