๐Ÿ“ฆ How to manage Python libraries when you move a project to another PC

 

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
๐Ÿ”— Do you like Techelopment? Check out the site for all the details!

๐Ÿ” 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:

The pip 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 .exe does 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