Python实现类库安装指定目录的示例详解

Written by

in

文章目录
  • import sys sys.path.insert(0, ‘/gemine/code/python311’) # 现在可以导入该目录下的包 import flash_attn
  • import sys import os # 自动添加自定义库路径 TARGET_DIR = ‘/gemine/code/python311’ if TARGET_DIR not in sys.path: sys.path.insert(0, TARGET_DIR) print(f”已添加 {TARGET_DIR} 到 Python 路径”) # 验证 try: import flash_attn print(f”flash-attn 加载成功,版本: {flash_attn.__version__}”) except ImportError as e: print(f”加载失败: {e}”)
  • # 一键添加(通用方法) python << ‘EOF’ import site import os target_dir = “/gemine/code/python311” site_packages = site.getsitepackages()[0] pth_file = os.path.join(site_packages, “custom_lib.pth”) # 写入 .pth 文件 with open(pth_file, ‘w’) as f: f.write(target_dir + ‘n’) print(f”已创建: {pth_file}”) print(f”内容: {target_dir}”) EOF
  • # 检查 Python 路径 python -c “import sys; print(‘/gemine/code/python311’ in sys.path)” # 检查能否导入 flash-attn python -c “import flash_attn; print(flash_attn.__version__)”
  • 场景 推荐方法 临时测试 方法 1(sys.path.insert) 个人长期使用 方法 2.1(~/.bashrc) 项目独立配置 方法 2.2(.pth 文件) 脚本自动化 方法 3(代码中自动添加) 最推荐的是方法 2.2(.pth 文件),因为它: 永久生效 不影响系统环境变量 只对 Python 生效,不干扰其他程序 需要我帮你写个一键配置脚本吗? 到此这篇关于Python实现类库安装指定目录的示例详解的文章就介绍到这了,更多相关Python类库安装指定目录内容请搜索风君子博客以前的文章或继续浏览下面的相关文章希望大家以后多多支持风君子博客! 您可能感兴趣的文章: Python的pandas类库结构参数 python excel使用xlutils类库实现追加写功能的方法 Python中工作日类库Busines Holiday的介绍与使用 python logging类库使用例子 Python读取图片EXIF信息类库介绍和使用实例
  • 目录
    • 方法 1:临时添加(单次运行)
    • 方法 2:永久添加(推荐)
      • 2.1 添加到环境变量(~/.bashrc)
      • 2.2 添加到 Python 启动文件
      • 2.3 添加到 conda 环境(如果用 conda)
    • 方法 3:Python 脚本中自动处理
      • 方法 4:使用 .pth 文件(最干净)
        • 验证是否生效
          • 推荐方案

            示例代码:

              TARGET_DIR="/gemine/code/python311"
            
            
            wget https://github.com/Dao-AILab/flash-attention/releases/download/v2.7.0.post1/flash_attn-2.7.0.post1+cu12torch2.4cxx11abiFALSE-cp311-cp311-linux_x86_64.whl
            
            pip install flash_attn-2.6.3+cu118torch2.4cxx11abiFALSE-cp311-cp311-linux_x86_64.whl 
                --target=$TARGET_DIR 
                --no-deps 
                --no-build-isolation
            

            TARGET_DIR="/gemine/code/python311" 添加到 Python 类库路径,有以下几种方法:

            import sys
            sys.path.insert(0, '/gemine/code/python311')
            
            # 现在可以导入该目录下的包
            import flash_attn
            

            # 编辑 bashrc
            echo 'export PYTHONPATH="/gemine/code/python311:$PYTHONPATH"' >> ~/.bashrc
            
            # 立即生效
            source ~/.bashrc
            

            # 找到 Python 站点包目录
            python -m site --user-site
            
            # 创建 .pth 文件(假设输出是 /home/user/.local/lib/python3.11/site-packages)
            echo "/gemine/code/python311" > /home/user/.local/lib/python3.11/site-packages/custom_path.pth
            

            # 找到当前环境的 site-packages
            python -c "import site; print(site.getsitepackages()[0])"
            
            # 创建 .pth 文件
            echo "/gemine/code/python311" > $(python -c "import site; print(site.getsitepackages()[0])")/custom_lib.pth
            

            import sys
            import os
            
            # 自动添加自定义库路径
            TARGET_DIR = '/gemine/code/python311'
            
            if TARGET_DIR not in sys.path:
                sys.path.insert(0, TARGET_DIR)
                print(f"已添加 {TARGET_DIR} 到 Python 路径")
            
            # 验证
            try:
                import flash_attn
                print(f"flash-attn 加载成功,版本: {flash_attn.__version__}")
            except ImportError as e:
                print(f"加载失败: {e}")
            

            # 一键添加(通用方法)
            python << 'EOF'
            import site
            import os
            
            target_dir = "/gemine/code/python311"
            site_packages = site.getsitepackages()[0]
            pth_file = os.path.join(site_packages, "custom_lib.pth")
            
            # 写入 .pth 文件
            with open(pth_file, 'w') as f:
                f.write(target_dir + 'n')
            
            print(f"已创建: {pth_file}")
            print(f"内容: {target_dir}")
            EOF
            

            # 检查 Python 路径
            python -c "import sys; print('/gemine/code/python311' in sys.path)"
            
            # 检查能否导入 flash-attn
            python -c "import flash_attn; print(flash_attn.__version__)"
            

            场景 推荐方法
            临时测试 方法 1(sys.path.insert)
            个人长期使用 方法 2.1(~/.bashrc)
            项目独立配置 方法 2.2(.pth 文件)
            脚本自动化 方法 3(代码中自动添加)

            最推荐的是方法 2.2(.pth 文件),因为它:

            • 永久生效
            • 不影响系统环境变量
            • 只对 Python 生效,不干扰其他程序

            需要我帮你写个一键配置脚本吗?

            到此这篇关于Python实现类库安装指定目录的示例详解的文章就介绍到这了,更多相关Python类库安装指定目录内容请搜索风君子博客以前的文章或继续浏览下面的相关文章希望大家以后多多支持风君子博客!

            您可能感兴趣的文章:

            • Python的pandas类库结构参数
            • python excel使用xlutils类库实现追加写功能的方法
            • Python中工作日类库Busines Holiday的介绍与使用
            • python logging类库使用例子
            • Python读取图片EXIF信息类库介绍和使用实例

            站内搜索