![]() |
When programming in Python, it is normal to have to install external libraries using pip.
Sooner or later, however, a fundamental question arises:
What happens when I port my Python program to another PC?
In this article, we will see:
- what actually happens
- why the program can stop working
- where to run the commands (
pip,venv, etc.) - the correct solutions based on the type of project
๐ The basic problem
The libraries installed with pip:
- are not part of the code
- are installed in the computer's Python environment
If you copy only the .py files to another PC, the program:
- will run only if all the required libraries are already installed
- Otherwise, it will give errors like:
ModuleNotFoundError: No module named 'library_name'
๐ Typical Python project structure
Example:
my_project/
│
├── main.py
├── utils.py
├── requirements.txt
└── venv/ (optional)
๐ All the commands we will see must be run in the project folder, i.e. my_project/, unless otherwise specified. indication.
✅ Solution 1: requirements.txt (standard method)
This is the most common method for sharing a Python project.
๐ Where to run the commands
๐ In the project folder
cd path/my_project
๐น Create requirements.txt
On the development PC:
pip freeze > requirements.txt
This file will contain the complete list of libraries used, for example:
requests==2.31.0
numpy==1.26.2
pandas==2.1.3
๐ The requirements.txt file should be copied with the project.
NOTE:
Thepip freeze command only works after the modules have been installed (already present in the environment). Therefore, it is necessary to run the command as soon as the Python application is up and running (i.e., if you run it on an environment where the libraries are not already installed, the requirements.txt file will be empty).
๐น Installing the libraries on a new PC
After copying the project:
cd path/my_project
pip install -r requirements.txt
๐ It's essential to be in the same folder as requirements.txt.
✔ Pros and ❌ Cons
Pros
- Simple
- Standard
- Used everywhere
Cons
- Requires Python installed
- Requires using the terminal
MEMO
Even if you run the command in your project folder, Python will create a file containing all the libraries currently installed on your system. So keep in mind that in this case, you're creating a requirements.txt file that doesn't contain only the libraries your program needs.
How to work around this: with virtual environments (venv)
๐งช Solution 2: Virtual Environment (venv)
A virtual environment isolates a project's libraries from system libraries.
๐น Creating the Virtual Environment
๐ In the project folder
cd my_project
python -m venv venv
Result:
my_project/
├── venv/
├── main.py
└── requirements.txt
๐น Activating the Environment virtual
Windows
venv\Scripts\activate
Linux / macOS
source venv/bin/activate
๐ After activation, all pip install will act only within the project.
๐น Installing libraries
pip install -r requirements.txt
✔ Pros and ❌ Cons
Pros
- No conflicts between projects
- Method professional
Cons
- Requires some experience
- Not ideal for end users
๐ Solution 3: Create an executable (PyInstaller)
This is the ideal solution if:
- You want to distribute the program to non-technical users
- You don't want them to install Python or libraries
๐น Install PyInstaller
๐ Anywhere, but recommended inside the project or in venv:
pip install pyinstaller
๐น Create the executable
๐ In the project folder
pyinstaller --onefile main.py
Result:
dist/
└── main.exe
๐ The executable:
- contains Python
- contains all libraries
- also works on PCs without Python
⚠ Important note
- The executable must be created for each operating system
- A Windows
.exedoes not work on macOS or Linux
๐ง Final summary
| Scenario | Solution | Where to launch the commands |
|---|---|---|
| Share code | requirements.txt |
Project folder |
| Structured Project | venv + requirements.txt |
Project Folder |
| End Users | PyInstaller | Project Folder |
✅ Conclusion
When porting a Python program to another PC:
- Code alone is not enough
- Libraries must be reinstalled or included
๐ The project folder is the main reference point for almost all Python programs. commands.
Follow me #techelopment
Official site: www.techelopment.it
facebook: Techelopment
instagram: @techelopment
X: techelopment
Bluesky: @techelopment
telegram: @techelopment_channel
whatsapp: Techelopment
youtube: @techelopment
