Skip to main content
TopMiniSite

Back to all posts

How to Get Pyinstaller to Include Imported .Py Files?

Published on
3 min read
How to Get Pyinstaller to Include Imported .Py Files? image

Best Python Development Tools to Buy in October 2025

1 Python Tools for Scientists: An Introduction to Using Anaconda, JupyterLab, and Python's Scientific Libraries

Python Tools for Scientists: An Introduction to Using Anaconda, JupyterLab, and Python's Scientific Libraries

BUY & SAVE
$39.81 $49.99
Save 20%
Python Tools for Scientists: An Introduction to Using Anaconda, JupyterLab, and Python's Scientific Libraries
2 Python Data Science Handbook: Essential Tools for Working with Data

Python Data Science Handbook: Essential Tools for Working with Data

BUY & SAVE
$44.18 $79.99
Save 45%
Python Data Science Handbook: Essential Tools for Working with Data
3 Learning Python: Powerful Object-Oriented Programming

Learning Python: Powerful Object-Oriented Programming

BUY & SAVE
$64.27 $79.99
Save 20%
Learning Python: Powerful Object-Oriented Programming
4 Effective Python Development for Biologists: Tools and techniques for building biological programs

Effective Python Development for Biologists: Tools and techniques for building biological programs

BUY & SAVE
$34.10 $39.00
Save 13%
Effective Python Development for Biologists: Tools and techniques for building biological programs
5 Programming Computer Vision with Python: Tools and algorithms for analyzing images

Programming Computer Vision with Python: Tools and algorithms for analyzing images

BUY & SAVE
$28.99 $59.99
Save 52%
Programming Computer Vision with Python: Tools and algorithms for analyzing images
6 Deep Learning with PyTorch: Build, train, and tune neural networks using Python tools

Deep Learning with PyTorch: Build, train, and tune neural networks using Python tools

BUY & SAVE
$34.40 $49.99
Save 31%
Deep Learning with PyTorch: Build, train, and tune neural networks using Python tools
7 Python Data Science Handbook: Essential Tools for Working with Data

Python Data Science Handbook: Essential Tools for Working with Data

  • COMPREHENSIVE GUIDE COVERING ESSENTIAL PYTHON DATA TOOLS.
  • HANDS-ON EXAMPLES FOR REAL-WORLD DATA ANALYSIS APPLICATIONS.
  • EXPERT INSIGHTS ON DATA VISUALIZATION AND MACHINE LEARNING TECHNIQUES.
BUY & SAVE
$74.53
Python Data Science Handbook: Essential Tools for Working with Data
8 Introducing Python: Modern Computing in Simple Packages

Introducing Python: Modern Computing in Simple Packages

BUY & SAVE
$30.99 $55.99
Save 45%
Introducing Python: Modern Computing in Simple Packages
9 Python Polars: The Definitive Guide: Transforming, Analyzing, and Visualizing Data with a Fast and Expressive DataFrame API

Python Polars: The Definitive Guide: Transforming, Analyzing, and Visualizing Data with a Fast and Expressive DataFrame API

BUY & SAVE
$64.51 $79.99
Save 19%
Python Polars: The Definitive Guide: Transforming, Analyzing, and Visualizing Data with a Fast and Expressive DataFrame API
10 Modern Python Cookbook: 130+ updated recipes for modern Python 3.12 with new techniques and tools

Modern Python Cookbook: 130+ updated recipes for modern Python 3.12 with new techniques and tools

BUY & SAVE
$25.11 $54.99
Save 54%
Modern Python Cookbook: 130+ updated recipes for modern Python 3.12 with new techniques and tools
+
ONE MORE?

To get PyInstaller to include imported .py files, you can use the --add-data flag when running PyInstaller. This flag allows you to specify additional files or directories to include in the bundled executable. You can use wildcards to include all files with a certain extension or in a specific directory. By using this flag, you can ensure that all imported .py files are included in the final executable generated by PyInstaller.

How can I force pyinstaller to include specific imported .py files?

To force PyInstaller to include specific imported .py files, you can use the --hidden-import flag to explicitly specify the modules or packages that you want to include in the bundled executable.

For example, if you have a Python script that imports a module named example_module.py, you can force PyInstaller to include this module by running the following command:

pyinstaller --onefile --hidden-import=example_module your_script.py

This will ensure that example_module.py is included in the bundled executable generated by PyInstaller.

You can also use the --hidden-import flag multiple times to include multiple imported modules or packages:

pyinstaller --onefile --hidden-import=module1 --hidden-import=module2 your_script.py

By explicitly specifying the imported modules using the --hidden-import flag, you can force PyInstaller to include them in the bundled executable, even if they are not directly imported in the main script.

How to get pyinstaller to include imported .py files?

  1. Create a spec file for PyInstaller by typing in the terminal:

pyi-makespec yourscript.py

Replace yourscript.py with the name of your main script file that imports other .py files.

  1. Open the spec file (yourscript.spec) in a text editor and add the paths of the imported .py files under the Analysis section:

a = Analysis(['yourscript.py', 'imported_file1.py', 'imported_file2.py'], pathex=['path/to/your/script'], binaries=None, datas=None, hiddenimports=[], hookspath=None)

  1. Run PyInstaller with the spec file:

pyinstaller yourscript.spec

This will include the imported .py files when creating the executable.

What is the preferred method for organizing imported .py files in the project directory to work seamlessly with pyinstaller?

The preferred method for organizing imported .py files in a project directory to work seamlessly with pyinstaller is to create a setup.py file. The setup.py file should include all necessary packages and modules that need to be bundled and compiled by pyinstaller.

Here is an example of how to organize imported .py files in a project directory with a setup.py file:

  1. Create a setup.py file in the root directory of your project.
  2. In the setup.py file, import the necessary modules and packages that need to be included in the final executable. For example:

from setuptools import setup

setup( name='my_project', version='1.0', packages=['my_package'], install_requires=[ 'numpy', 'pandas', 'matplotlib', ], )

  1. Ensure that the file structure of your project directory is organized in a way that the setup.py file can easily locate and include all necessary modules and packages.
  2. Run the following command in the terminal to build the executable using pyinstaller:

pyinstaller --onefile my_script.py

By organizing your project directory and imported .py files in this way, pyinstaller will be able to easily package and compile your project into a single executable file that includes all dependencies.