如何将Python依赖安装至项目目录,使应用可直接分享运行无需额外下载?
Great question! This is a super common need for keeping projects self-contained and shareable. Let’s break this down into two clear parts: getting dependencies installed locally to your project (instead of the C:\ system Python folder), and packaging everything so others can run your app without any setup.
Part 1: Install Dependencies Directly in Your Project Folder
The most clean and reliable approach here is using a virtual environment—it isolates your project’s dependencies from the system-wide Python installation, so you don’t clutter up your C:\ drive or cause version conflicts between projects. Here’s how to set it up on Windows:
- First, create your project folder and open a Command Prompt or PowerShell window inside it.
- Create a virtual environment (we’ll use
.venvas the folder name to keep it hidden and unobtrusive):python -m venv .venv - Activate the virtual environment—this tells your system to use the project-local Python installation instead of the system one:
- For Command Prompt:
.venv\Scripts\activate.bat - For PowerShell (you may need to enable script execution first via
Set-ExecutionPolicy RemoteSigned):.venv\Scripts\Activate.ps1
(.venv)at the start of your command line. - For Command Prompt:
- Now install your dependencies (like Django or MySQLclient)—they’ll be stored in the
.venvfolder inside your project, not the system Python directory:pip install django mysqlclient - To save a list of your dependencies for future reference or sharing, export them to a
requirements.txtfile:pip freeze > requirements.txt
If you don’t want to use a virtual environment (not recommended for most projects), you can directly install packages to a project folder using the --target flag:
pip install django mysqlclient --target ./project-deps
To run your app using these local dependencies, you’ll need to add the folder to your Python path first:
- Command Prompt:
set PYTHONPATH=%PYTHONPATH%;./project-deps - PowerShell:
$env:PYTHONPATH += ";./project-deps"
Then run your script as usual.
Part 2: Package Your App & Dependencies into a Standalone Executable
To let others run your app without installing Python or any dependencies, use a tool like PyInstaller to bundle everything into a single executable file. Here’s how:
- Make sure you’re still in your activated virtual environment, then install PyInstaller:
pip install pyinstaller - For a simple script (e.g.,
my_script.py), run this command to package it into a single.exe:
The final executable will appear in thepyinstaller --onefile my_script.pydistfolder of your project—users can double-click it to run your app immediately.
For Django Projects (Extra Steps)
Django apps have static files and templates that need to be included in the bundle:
- First, collect all static files into a single folder:
python manage.py collectstatic - When running PyInstaller, specify these additional files to include. You can either:
- Use flags to add static and template folders directly:
pyinstaller --onefile --add-data "static;static" --add-data "templates;templates" manage.py - Or generate a
.specfile, edit it to include your project’s files, then build from that:
Open the generatedpyinstaller --name my_django_app manage.pymy_django_app.specfile, add your static/template paths to thedataslist, then run:pyinstaller my_django_app.spec
- Use flags to add static and template folders directly:
- The final executable in the
distfolder will include everything users need to run your Django app—no Python installs required.
内容的提问来源于stack exchange,提问作者piyush salunke

