# server and shell using different python versions

**URL:** <https://forum.djangoproject.com/t/server-and-shell-using-different-python-versions/9303>\
**Category:** Using Django\
**Created:** [August 20, 2021, 2:32am UTC](https://forum.djangoproject.com/t/server-and-shell-using-different-python-versions/9303 "2021-08-20T02:32:44Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![JeffreyBenjaminBrown](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/jeffreybenjaminbrown/32/2237_2.png) [@JeffreyBenjaminBrown](https://forum.djangoproject.com/u/JeffreyBenjaminBrown)\
**Post date:** [August 20, 2021, 2:32am UTC](https://forum.djangoproject.com/t/server-and-shell-using-different-python-versions/9303/1 "2021-08-20T02:32:44Z")

</div>

The Django REPL is running python 3.8. I know that because when I run `python3 manage.py shell`, I can do this:

```auto
In [1]: import sys

In [2]: sys.version
Out[2]: '3.8.2 (default, Mar 26 2020, 15:53:00) \n[GCC 7.3.0]'

```

But the server is using 3.7. I know that because when I load this view:

```auto
def write_python_version ( request ):
  return render (
    request,
    'run_make/write_python_version.html',
    { "version" : str ( sys.version ) } )

```

with this template:

```auto
The Python version is:
  {{version}}<br>

```

the page reads:

```auto
The Python version is: 3.7.3 (default, Jan 22 2021, 20:04:44) [GCC 8.3.0]

```

That seems bad on its face, but I would ignore it, if it weren’t for the fact that I think it’s preventing me from importing Pandas. When I try to import it, in Debug Mode, the page shows the following NumPy error. (I never actually tried to import NumPy directly, but I believe Pandas is trying to do so.)

```auto
numpy:

IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!

Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.

We have compiled some common reasons and troubleshooting tips at:

    https://numpy.org/devdocs/user/troubleshooting-importerror.html

Please note and check the following:

  * The Python version is: Python3.7 from "/usr/bin/python3"
  * The NumPy version is: "1.20.2"

and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [August 20, 2021, 3:34am UTC](https://forum.djangoproject.com/t/server-and-shell-using-different-python-versions/9303/2 "2021-08-20T03:34:41Z")

</div>

How are you running the shell? Do you have a virtual environment active? What do you get if you just run a python3 -V? How about python -V?

How are you running the server?

---

<div class="post-metadata">

**Author:** ![JeffreyBenjaminBrown](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/jeffreybenjaminbrown/32/2237_2.png) [@JeffreyBenjaminBrown](https://forum.djangoproject.com/u/JeffreyBenjaminBrown)\
**Post date:** [August 20, 2021, 4:43pm UTC](https://forum.djangoproject.com/t/server-and-shell-using-different-python-versions/9303/3 "2021-08-20T16:43:56Z")

</div>

Thanks for responding, Ken!

I run the shell with `python3 manage.py shell`, from the project root. I start the server with `service apache2 start`.

Python 3.8 is provided by Conda, while 3.7 is not:

```auto
appuser@127:/mnt/django$ which python3
/opt/conda/bin/python3
appuser@127:/$ which python3.7
/usr/bin/python3.7

```

I’m using the Docker image described [here](https://github.com/ofiscal/tax.co/blob/master/docker/Dockerfile). That doesn’t (explicitly?) install python3, but it does `RUN apt install -y python3-pip`. Maybe that installs a competing version of Python?

It looks like [Conda + Pip can lead to trouble](https://www.anaconda.com/blog/using-pip-in-a-conda-environment). But I need some packages that aren’t packaged for Conda, and I don’t want to have to package them myself. I hope that’s not the only way forward.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [August 20, 2021, 4:55pm UTC](https://forum.djangoproject.com/t/server-and-shell-using-different-python-versions/9303/4 "2021-08-20T16:55:47Z")

</div>

What do you want your “official” python installation to be? The Conda environment, the system environment (discouraged), or a virtual environment?

If the Conda-based environment is your preferred environment, then you want to ensure that your apache configuration is using _it_ for its python runtime, not the system default installation.

Adding docker to the mix does add another level of complexity. If you’re building everything on the Conda packages, then you probably want to create your container using that as well.

But I think the key item here is that you want to make sure you’re consistent from top to bottom. Build one virtual environment for your system and ensure that that’s what you use in all situations.

(We ditched Conda a couple years ago, outside some very specific and limited applications. Its quirks were more than what we wanted to deal with and we never faced the problems it directly addresses as being problems for us.)

---

<div class="post-metadata">

**Author:** ![JeffreyBenjaminBrown](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/jeffreybenjaminbrown/32/2237_2.png) [@JeffreyBenjaminBrown](https://forum.djangoproject.com/u/JeffreyBenjaminBrown)\
**Post date:** [August 20, 2021, 7:48pm UTC](https://forum.djangoproject.com/t/server-and-shell-using-different-python-versions/9303/5 "2021-08-20T19:48:28Z")

</div>

I’d like Conda to be the default. In my `apache2.conf` file I’ve got the following line:

```auto
WSGIDaemonProcess www-data python-path=/mnt/django:/opt/conda/lib/python3.8/site-packages/

```

which doesn’t even say to look in `/usr/bin`, the place Apache is finding Python 3.7. I’ve looked around online and can’t find anything else that says how to tell Apache which Python to use. Do you know?

> Adding docker to the mix does add another level of complexity. If you’re building everything on the Conda packages, then you probably want to create your container using that as well.

I don’t understand. All my code runs in the Docker container. When I build the image for that container, I install Conda first, and every package that can be conda-installed. Then I use pip3 to install a few things that aren’t packaged for Conda.

> Build one virtual environment for your system and ensure that that’s what you use in all situations.

I think that’s what I’m doing, by doing everything inside the docker container. It’s just that the docker container’s got two versions of python3.

---

<div class="post-metadata">

**Author:** ![JeffreyBenjaminBrown](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/jeffreybenjaminbrown/32/2237_2.png) [@JeffreyBenjaminBrown](https://forum.djangoproject.com/u/JeffreyBenjaminBrown)\
**Post date:** [August 20, 2021, 7:51pm UTC](https://forum.djangoproject.com/t/server-and-shell-using-different-python-versions/9303/6 "2021-08-20T19:51:18Z")

</div>

When I enter the container as root and as it to `apt remove python3.7`, I’m told that

```auto
The following packages were automatically installed and are no longer required:
  gir1.2-glib-2.0 iptables libexpat1-dev libgirepository-1.0-1 libip4tc0 libip6tc0 libiptc0
  libmpdec2 libnetfilter-conntrack3 libnfnetlink0 libnftables0 libnftnl11 libpython3-dev
  libpython3-stdlib libpython3.7 libpython3.7-dev libpython3.7-minimal libpython3.7-stdlib
  nftables python-pip-whl python3-minimal python3.7-minimal
Use 'apt autoremove' to remove them.
The following packages will be REMOVED:
  dh-python libapache2-mod-wsgi-py3 python3 python3-asn1crypto python3-cffi-backend
  python3-crypto python3-cryptography python3-dbus python3-dev python3-distutils
  python3-entrypoints python3-gi python3-keyring python3-keyrings.alt python3-lib2to3
  python3-pip python3-pkg-resources python3-secretstorage python3-setuptools python3-six
  python3-wheel python3-xdg python3.7 python3.7-dev ufw

```

So maybe it’s that a lot of the things I installed via `apt` caused `python3.7` to be installed? I don’t recognize a lot of them, but `libapache2-mod-wsgi-py3` and `python3-pip` are crucial.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [August 20, 2021, 8:16pm UTC](https://forum.djangoproject.com/t/server-and-shell-using-different-python-versions/9303/7 "2021-08-20T20:16:17Z")

</div>

> [@JeffreyBenjaminBrown](#):
>
> `WSGIDaemonProcess`

> [@JeffreyBenjaminBrown](#):
>
> I’ve looked around online and can’t find anything else that says how to tell Apache which Python to use. Do you know?

It’s the `python-home` directive.

I’m assuming you already have this link, but for reference: [WSGIDaemonProcess — mod\_wsgi 5.0.0 documentation](https://modwsgi.readthedocs.io/en/develop/configuration-directives/WSGIDaemonProcess.html)

From the section on python-home:

> Note that the Python virtual environment must have been created using the same base Python version as was used to compile the mod\_wsgi module. You can’t use this to force mod\_wsgi to somehow use a different Python version than it was compiled for. If you want to use a different version of Python, you will need to reinstall mod\_wsgi, compiling it for the version you want.

Adding the Conda libraries just to python-path _is_ going to create a mix of versions between libraries and the python runtime, and anything with a compiled component is likely to have problems in that environment.

---

<div class="post-metadata">

**Author:** ![JeffreyBenjaminBrown](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/jeffreybenjaminbrown/32/2237_2.png) [@JeffreyBenjaminBrown](https://forum.djangoproject.com/u/JeffreyBenjaminBrown)\
**Post date:** [August 22, 2021, 3:23pm UTC](https://forum.djangoproject.com/t/server-and-shell-using-different-python-versions/9303/8 "2021-08-22T15:23:49Z")

</div>

Thanks, Ken. I determined that installing any of ufw, mod-wsgi, apache or pip via apt causes Python 3.7 to be installed, so I installed them all by hand. It seemed to work, but when I try to start Apache it complains that it can’t find `libpcre.so.1`. But libpcre is in fact installed.

I had forgotten how hard it was to set up Apache in the first place. To avoid another week of playing whackamole I’m planning to just let the version conflict persist, and use the `csv` library instead of `pandas` from Django. I don’t (from Django) need anything fancy that would actually require Pandas. I do from the microsimulation that Django runs, but it uses a syscall to start another python environment which has access to it.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [August 22, 2021, 4:17pm UTC](https://forum.djangoproject.com/t/server-and-shell-using-different-python-versions/9303/9 "2021-08-22T16:17:20Z")

</div>

I can understand where using mod\_python may be the easiest way of doing it - as long as you have a rather “vanilla” installation. But once you get outside the norm, things can get hairy rather quickly.

We had the situation where different apps (non-Django) were using different versions of Python, running under the same server at the same time.

I’m reasonably certain that that is why we originally adopted using the mod\_wsgi / uwsgi pair rather than mod\_python. (We also found it useful to be able to manage those different applications independently rather than having them all tied to the apache instance.)

Getting used to working with uwsgi also made the transition to nginx rather trivial.

---

<div class="post-metadata">

**Author:** ![Jason](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/jason/32/5523_2.png) [@Jason](https://forum.djangoproject.com/u/Jason)\
**Post date:** [August 24, 2021, 3:34am UTC](https://forum.djangoproject.com/t/server-and-shell-using-different-python-versions/9303/10 "2021-08-24T03:34:38Z")

</div>

Hey Jeffery, in case you haven’t 100% given up, did you happen to try any of the approaches in the link beow? In particular, `update-alternatives` might be helpful since you’re apparently running your app in a Debian-based distribution.

> **[How to change from default to alternative Python version on Debian Linux](https://linuxconfig.org/how-to-change-from-default-to-alternative-python-version-on-debian-linux)**
>
> How to change from default to alternative Python version on Debian Linux
