1

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} "$@"
charlottesville
  • 467
  • 5
  • 19
  • I wonder what is inside the `/home/centos/blueflow/blueflow/bin/blueflow-django-manage` – Niko Föhr Feb 23 '22 at 11:26
  • I updated this to include the contents of `/home/centos/blueflow/blueflow/bin/blueflow-django-manage` – charlottesville Feb 23 '22 at 12:03
  • If you're trying to manually manage the packets that are in your django environment, you need to get rid of the `DJANGO_MANAGE="${PIPENV} run python ${BLUEFLOW_HOME}/app/manage.py"` in the `blueflow-django-manage` script, as [pipenv](https://github.com/pypa/pipenv) *automatically creates and manages a virtualenv for your projects*. You could try something like `DJANGO_MANAGE="$python ${BLUEFLOW_HOME}/app/manage.py"`, but I still would recommend using at least normal `venv` virtual environments. – Niko Föhr Feb 23 '22 at 12:08
  • What is the advantage of "but I still would recommend using at least normal venv virtual environments"? I just want to run a command that will allow the software to find the packages that have been installed. What is the easiest way to do that? – charlottesville Feb 26 '22 at 11:13
  • Easiest way to do it is to use same `python` executable to install the packages (`python -m pip install ...`) and then to run your script (`python myscript.py`). No other magic is needed. – Niko Föhr Feb 26 '22 at 11:17
  • Okay, so, as you can see, I installed everything with pip3, so I tried python3 and that did seem to get me past some install issues. – charlottesville Feb 27 '22 at 13:42

1 Answers1

1

You can add any path like this:

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

Note, that this is not the recommended way, but rather a fallback option.

You could also do print(sys.path) and print(sys.executable) (taken from here) to find out which python you are actually executing.

cknoll
  • 2,130
  • 4
  • 18
  • 34