You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Python依赖安装至项目目录,使应用可直接分享运行无需额外下载?

How to Install Python Dependencies in Your Project Folder & Package for Easy Sharing

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 .venv as 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
      
    You’ll know it’s active when you see (.venv) at the start of your command line.
  • Now install your dependencies (like Django or MySQLclient)—they’ll be stored in the .venv folder 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.txt file:
    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:
    pyinstaller --onefile my_script.py
    
    The final executable will appear in the dist folder 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:

  1. First, collect all static files into a single folder:
    python manage.py collectstatic
    
  2. 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 .spec file, edit it to include your project’s files, then build from that:
      pyinstaller --name my_django_app manage.py
      
      Open the generated my_django_app.spec file, add your static/template paths to the datas list, then run:
      pyinstaller my_django_app.spec
      
  3. The final executable in the dist folder will include everything users need to run your Django app—no Python installs required.

内容的提问来源于stack exchange,提问作者piyush salunke

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:20:44