在不考虑 virtualenv 的情况下,强制 Python 查找模块的快速方法是什么?

What is a fast way to force Python to find a module, without regard to virtualenv?

我正在接手一个项目。 5位工程师为此工作了几年,但他们都走了。我的任务是尝试恢复这个项目并让它继续下去。这是一个很大的 Python 项目,有几个复杂的安装脚本,现在有很多版本错误,因为 3 或 4 年前工作的东西早就被弃用了,可能已经停产了。

隐藏在许多安装脚本之一中(它们都互相调用多次,在我无法理解的意大利面条中)可能有一条设置虚拟环境的指令,但我找不到这条线,我不在乎。该软件将在我完全控制的 EC2(带有 Centos 7)上进行全新安装。这个软件是唯一一个 运行 在这个 EC2 实例上的软件,所以我很乐意在全球范围内安装所有东西。

安装脚本无法找到 Python 3.6,所以我手动这样做了:

pip3 install astroid
pip3 install cycler
pip3 install decorator
pip3 install fancycompleter
pip3 install ipython
pip3 install isort
pip3 install kiwisolver
pip3 install lazy-object-proxy
pip3 install matplotlib
pip3 install numpy
pip3 install pillow
pip3 install platformdirs
pip3 install pluggy
pip3 install prompt-toolkit
pip3 install pygments
pip3 install pyparsing
pip3 install pytest
pip3 install tomli
pip3 install typed-ast
pip3 install typing-extensions
pip3 install asgiref
pip3 install charset-normalizer
pip3 install click
pip3 install django
pip3 install django-timezone-field
pip3 install idna
pip3 install markdown
pip3 install markupsafe
pip3 install pyodbc
pip3 install python-nmap
pip3 install pyyaml
pip3 install sqlparse
pip3 install uritemplate

如果我进入 Python shell 和 运行 help(modules) 我可以看到 Django 已安装。

但是当我运行最终安装脚本时:

/home/centos/blueflow/blueflow/bin/blueflow-django-manage compilejsx -o ./blueflow/jsx/JSXRegistry.jsx

部分错误如下:

Couldn't import Django. Are you sure it's installed 

获取代码以查看 Django 正确路径的最快方法是什么?有没有一种明显的方法可以脱离所有虚拟环境并强制代码对所有内容使用全局环境(我认为这是最简单的方法)?

Courtesy Notice: Pipenv found itself running within a virtual environment, so it will automatically use that environment, instead of creating its own for any project. You can set PIPENV_IGNORE_VIRTUALENVS=1 to force pipenv to ignore that environment and create its own instead. You can set PIPENV_VERBOSITY=-1 to suppress this warning.
Loading .env environment variables...
Warning: There was an unexpected error while     activating your virtualenv. Continuing anyway...
Traceback (most recent call last):
 File "/home/centos/blueflow/blueflow/app/manage.py", line 20, in <module>
"Couldn't import Django. Are you sure it's installed and "
ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?

[[更新]]

有人问内容:

/home/centos/blueflow/blueflow/bin/blueflow-django-manage

这是整个文件:

#!/bin/bash
#
# blueflow-django-manage
#
# Shim for Django's "python manage.py" that supports different plaforms

# Copyright (C) 2018 Virta Laboratories, Inc.  All rights reserved.

# Stop on errors
set -e

# Path to BlueFlow installation
BLUEFLOW_HOME=${BLUEFLOW_HOME:=`cd $(dirname [=16=])/.. && pwd -P`}

# Defaults
PIPENV=${BLUEFLOW_HOME}/bin/blueflow-pipenv
DJANGO_MANAGE="${PIPENV} run python ${BLUEFLOW_HOME}/app/manage.py"

# Platform-specific section
OS=`${BLUEFLOW_HOME}/bin/blueflow-os-detect`
case ${OS} in
  "Ubuntu-16.04")
    DJANGO_MANAGE="sudo -Eu blueflow ${DJANGO_MANAGE}"
  ;;
  "RHEL7")
    if [[ "${DJANGO_SETTINGS_MODULE}" != "app.settings.development" ]]; then
      DJANGO_MANAGE="sudo -Eu blueflow ${DJANGO_MANAGE}"
    else
      DJANGO_MANAGE="sudo -Eu $(id -un) ${DJANGO_MANAGE}"
    fi
  ;;
  "RHEL8")
    if [[ "${DJANGO_SETTINGS_MODULE}" != "app.settings.development" ]]; then
      DJANGO_MANAGE="sudo -Eu blueflow ${DJANGO_MANAGE}"
    else
      DJANGO_MANAGE="sudo -Eu $(id -un) ${DJANGO_MANAGE}"
    fi
  ;;
esac

# Replace this subshell with command
exec ${DJANGO_MANAGE} "$@"

您可以像这样添加任何路径:

import sys
sys.path.append("my/path/to/django")

请注意,这不是推荐的方式,而是一个后备选项。

您还可以执行 print(sys.path)print(sys.executable)(取自 here)来找出您实际执行的 python。