强制 Python 查找模块而不考虑 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 $0)/.. && 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} "$@"
I'm taking over a project. 5 engineers worked on this for several years, but they are all gone. I've been tasked with trying to revive this project and keep it going. It's a big Python project with several complicated install scripts which, nowadays, have many version errors, because the stuff that worked 3 or 4 years ago is all long since deprecated and possibly discontinued.
Buried deep in one of the many install scripts (they all call each other multiple times, in a spaghetti that I cannot figure out) there is probably an instruction that sets up a virtual environment, but I can't find the line and I don't care. This software is going onto a clean install of an EC2 (with Centos 7) that I control completely. And this piece of software is the only software that will ever run on this EC2 instance, so I'm happy to install everything globally.
The install script was unable to find Python 3.6 so I manually did this:
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
If I go into the Python shell and run help(modules)
I can see the Django is installed.
But when I run the final install script:
/home/centos/blueflow/blueflow/bin/blueflow-django-manage compilejsx -o ./blueflow/jsx/JSXRegistry.jsx
Part of the error reads:
Couldn't import Django. Are you sure it's installed
What would be the fastest way to get the code to see the right path to Django? Is there an obvious way I can break out of all virtual environments and force the code to use the global environment for everything (which I'm thinking would be the simplest approach)?
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?
[[ Update ]]
Someone asked about the contents of:
/home/centos/blueflow/blueflow/bin/blueflow-django-manage
This is the whole file:
#!/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 $0)/.. && 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} "$@"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以添加任何路径,如下所示:
请注意,这不是推荐的方式,而是后备选项。
您还可以执行
print(sys.path)
和print(sys.executable)
(取自 此处)找出您实际正在执行的Python。You can add any path like this:
Note, that this is not the recommended way, but rather a fallback option.
You could also do
print(sys.path)
andprint(sys.executable)
(taken from here) to find out which python you are actually executing.