1

My Ubuntu system is on python 2.7.15

conda install -c anaconda flask

Anaconda always installs python 3.5 with Flask and other packages. How can I not install python 3.7 and leave python 2.7.15 as is when installing anaconda packages?

GavinBelson
  • 2,514
  • 25
  • 36
  • I believe that is only installing packages to your current conda env (probably the default env) which I assume is python 3.7. You need to create another env with whatever python version you wnt an install there. – juanpa.arrivillaga Feb 05 '19 at 19:49
  • 1
    And your OS can house any number of python interpreters, it isn't "on Python 2.7.15", unless you mean that is the system Python, in which case, you probably shouldn't be messing with that anyway, and it shouldn't be related to your conda/anaconda python distribution – juanpa.arrivillaga Feb 05 '19 at 19:50
  • If you want to learn more about the concept of environments, here is a nice link: https://realpython.com/python-virtual-environments-a-primer/#using-different-versions-of-python – Felipe Bormann Feb 05 '19 at 19:53
  • 2
    @FelipeBormann The linked article covers the `virtualenv` python environment manager. `conda` itself also has [full environment management capabilities](https://conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html) though. If you're working with an Anaconda distribution, then personally I'd recommend to use `conda` instead of `virtualenv` for managing python environments. See also [Does Conda replace the need for virtualenv?](https://stackoverflow.com/questions/34398676/does-conda-replace-the-need-for-virtualenv) – Xukrao Feb 05 '19 at 20:13
  • Yes, this is on a an anacoda virtual environment. I would recommend using the anaconda virtual environment to pair with the anaconda installer https://conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html – GavinBelson Feb 05 '19 at 20:19
  • @Xukrao I agree. I used the link as a source for him to learn more about virtual environments as a concept itself, your link is of great use as well, thanks! – Felipe Bormann Feb 05 '19 at 20:58

1 Answers1

4

The Python you install with anaconda does not interfere at all with your system Python. You can use Anaconda to have multiple Pythons (in multiple conda environments) besides the system Python. You just have to make sure which one is invoked when you run scripts and make sure it's the one you intended.

To answer the "literal" question you asked, you can specify the Python version when installing something:

conda install -c anaconda flask python=2

This will keep your Python at version 2 or report a mismatch if the package you want to install isn't available on anaconda for Python 2. The number of packages dropping Python 2 support is increasing because Python 2 is near it's "end of life", so don't expect to get latest or even near-latest releases of the packages when keeping at Python 2.

Personally I would recommend to create a different environment instead of trying to install to much into the base environment:

conda create -n mypython2environment python=2 flask

And by activating that environment you should be able to use the packages you installed in that environment:

activate mypython2environment

Several IDEs have built-in support for conda environments, so these may be helpful (especially in making sure you use the correct environment and thus the correct Python).

MSeifert
  • 145,886
  • 38
  • 333
  • 352
  • Don't you need to create an env first? I've never directly passed the `python` argument to conda install. How does that work? – juanpa.arrivillaga Feb 05 '19 at 19:53
  • It's just specifying that additionally to `flask` you want to install `python=2`. If `python=2` is already satisfied it's ignored (except for "conflict resolution"). You can use it to downgrade/upgrade the Python version in that environment. – MSeifert Feb 05 '19 at 19:57
  • Ah, I see, although, I don't think it will work in the default environment, at least last time I tried this. – juanpa.arrivillaga Feb 05 '19 at 20:02
  • @juanpa.arrivillaga I would be surprised if it didn't work. At least currently Miniconda still provides a Python-2-based download (https://conda.io/en/latest/miniconda.html). – MSeifert Feb 05 '19 at 20:06
  • No I mean if you try to do that inside your default environment, it will fail. That is, you cannot change the python version of your default environment (last I checked, I just remember trying this when I wanted to upgrade my default environment and then realized the only solution is just to create another one) – juanpa.arrivillaga Feb 05 '19 at 20:09
  • 1
    Okay, that's interesting I just tried it locally and it worked (although it removed and downgraded several packages). Although my base environment is quite bare (just python, conda, pip and their dependencies). Maybe a problem in condas dependency resolver - I had that sometimes... – MSeifert Feb 05 '19 at 20:11
  • 1
    `python=2` works great. I do have a conda virtual env set up, with some Celery workers so I prefer to keep it all on the same python version. – GavinBelson Feb 05 '19 at 20:15