How to change Python version of existing conda virtual environment? - python

I created a conda environment with Python version 3.8, but it doesn't support matplotlib... So I am looking for something like this to change the Python version: conda env my_env update to python=3.6. Is this possible or do I need to recreate the environment?
I have miniconda installed.

Activate the relevant environment, then install your target python version.
conda activate my_env
conda install python=3.6

Adding to the answer above
conda activate my_env
conda uninstall python
conda install python=x.x

Rebuild a new environment, for example called "myenvi"
conda create --name myenvi python=3.6
And make sure the version by
python --version
After installing all packages, double-check with
conda list -n myenvi

If you already have existing python installation may be in other environment, you can simply use it as base.
Here's what you need to do:
conda activate base
conda install python=3.6
Note: This will activate root environment. and python 3.6 already installed, it will simply replace it.

Related

how to manage different module version with the same anaconda?

I am about to program 2 different python projects on my computer, each of them is using a different version of a specific module (PyTorch), as well as modules from the latest anaconda.
I have already installed anaconda and found out that the solution is a virtual environment, however, I don't want to install all anaconda modules for each one of them, but use the already installed anaconda for both of them.
How do I do it?
You can use virtual environment which allows you to install specific packages (with specific version) and/or specific python version.
From the docs,
To create virutal environment called myenv
conda create -n myenv
# Create with specific python version
conda create -n myenv python=3.6
# Create with specific version of python and package
conda carete -n myenv python=3.5 pytorch=1.2
To use a virtual env, you have to activate it.
# Activating myenv
conda activate myenv
To deactivate a env, and fall back to default anaconda env,
# myenv
conda deactivate
conda activate base
# Anaconda default env is called base
To list available virtual env
conda env list
or
conda info --envs

Add full anaconda package list to existing conda environment

I know how to add single packages and I know that the conda create command supports adding a new environment with all anaconda packages installed.
But how can I add all anaconda packages to an existing environment?
I was able to solve the problem as following:
Create a helper env with anaconda: conda create -n env_name anaconda
Activate that env conda activate env_name
Export packages into specification file: conda list --explicit > spec-file.txt
Activate the target environment: activate target_env_name
Import that specification file: conda install --file spec-file.txt

Setup a virtual environment for pip work and conda work

OS - Ubuntu 16.04 LTS
I want to setup my machine so I have two virtual environments
For pip
For conda
I want to do this so in one virtual environment I can have all libraries that are installed using pip and in the other I can have all my anaconda work/libraries
For pip and conda both I want to a have Python3.6 as default
How can I do this ?
First, install Miniconda. You can then create an environment where you (try to) only use conda
> conda create -n conda_only python=3.6
> conda install -n conda_only numpy ...
And you can also make your 'pip' environment as a conda env, but then only use pip to install packages. For example,
> conda create -n pip_only python=3.6 pip
> source activate pip_only
> pip install numpy ...
I don't know your motivation (benchmarking installation times perhaps?), but it should be noted that in practice it's common to mix conda and pip simply because some packages are only on PyPI. Plus, conda recognizes packages installed by pip.

conda install downgrade python version

I'm trying to downgrade python version of anaconda via conda install python=3.3, but have following error:
~/anaconda3/bin$ ./conda install python=3.3
Fetching package metadata .........
Solving package specifications: .
UnsatisfiableError: The following specifications were found to be in conflict:
- gevent -> python 2.6*
- python 3.3*
Use "conda info <package>" to see the dependencies for each package.
How to resolve conflicts with the packages?
If you want to set specific version, use it like this:
WARNING: This command will overwrite the default python version system-wise
conda install python=3.6
To create environment with a specific version, you can do:
conda create -n $PYTHON36_ENV_NAME python=3.6 anaconda # set custom env name
The anaconda at the end allows the env to use all anaconda packages
For more information refere to Anaconda documentation
There are two ways to downgrade python in anaconda.
1. Downgrade python in the active environment
(This can lead to conflicts with installed packages for higher python versions)
conda activate nameOfYourEnvironment
conda install python=3.3
2. Create a new environment
(This is a more safer way, but you need to install all necessary packages again)
conda activate base
conda create --name env_name python=3.3
Hint: Use conda list before creating a new environment to get the names of all installed packages in the actual environment.
If you want to check your installed environments do:
conda env list
If you got problems in installing, make sure to run the shell as administrator (always recommended).
You can make environments with other versions of Python using this command:
conda create --name py33 python=3.3
source activate py33
Very firstly check the current version using command python --version. Then on anaconda prompt type the command conda search python which will list all the python versions available till date. Then from that list select your version and type conda install python=3.5.2 or any of your choice

How do I upgrade to Python 3.6 with conda?

I want to get the latest version of Python to use f-strings in my code. Currently my version is (python -V):
Python 3.5.2 :: Anaconda 4.2.0 (x86_64)
How would I upgrade to Python 3.6?
Anaconda had not updated Python internally to 3.6, but later versions of Anaconda has a Python 3.6 version here.
a) Method 1
If you wanted to update, you will type conda update python
To update Anaconda, type conda update conda
If you want to upgrade between major python versions, like 3.5 to 3.6, you'll have to do
conda install python=$pythonversion$
b) Method 2 - Create a new environment (the better method)
conda create --name py36 python=3.6
c) To get the absolute latest Python (3.6.5 at time of writing)
conda create --name py365 python=3.6.5 --channel conda-forge
You can see all this from here.
Also, refer to this for force upgrading.
Creating a new environment will install Python 3.6:
conda create --name 3point6 python=3.6
Output:
Fetching package metadata .......
Solving package specifications: ..........
Package plan for installation in environment /Users/dstansby/miniconda3/envs/3point6:
The following NEW packages will be INSTALLED:
openssl: 1.0.2j-0
pip: 9.0.1-py36_1
python: 3.6.0-0
readline: 6.2-2
setuptools: 27.2.0-py36_0
sqlite: 3.13.0-0
tk: 8.5.18-0
wheel: 0.29.0-py36_0
xz: 5.2.2-1
zlib: 1.2.8-3
I found this page with detailed instructions to upgrade Anaconda to a major newer version of Python (from Anaconda 4.0+). First,
conda update conda
conda remove argcomplete conda-manager
I also had to conda remove some packages not on the official list:
backports_abc
beautiful-soup
blaze-core
Depending on packages installed on your system, you may get additional UnsatisfiableError errors. Simply add those packages to the remove list. Next, install the version of Python,
conda install python==3.6
which takes a while, after which a message indicated to conda install anaconda-client, so I did
conda install anaconda-client
which says it's already there. Finally, following the directions,
conda update anaconda
I did this in the Windows 10 command prompt, but things should be similar in Mac OS X.
In the past, I have found it quite difficult to try to upgrade in-place.
Note: my use-case for Anaconda is as an all-in-one Python environment. I don't bother with separate virtual environments. If you're using conda to create environments, this may be destructive because conda creates environments with hard-links inside your Anaconda/envs directory.
So if you use environments, you may first want to export your environments. After activating your environment, do something like:
conda env export > environment.yml
After backing up your environments (if necessary), you may remove your old Anaconda (it's very simple to uninstall Anaconda):
$ rm -rf ~/anaconda3/
and replace it by downloading the new Anaconda, e.g. Linux, 64 bit:
$ cd ~/Downloads
$ wget https://repo.continuum.io/archive/Anaconda3-4.3.0-Linux-x86_64.sh
(see here for a more recent one),
and then executing it:
$ bash Anaconda3-4.3.0-Linux-x86_64.sh
I'm using macOS v10.14 (Mojave).
These four steps worked for me.
conda update conda
conda install python=3.6
conda install anaconda-client
conda update anaconda
If you want to upgrade the Python version inside your existing environment, activate it first with conda activate <env_name> and then do:
conda install -c anaconda python=<version>
You might also need to update the dependencies with
conda update --all
This is how I manage to get (as currently there isn't any direct support. In the future, it will be for sure) Python 3.9 in earlier versions of Anaconda and Windows 10.
Note: I needed extra packages, so install them. Install only what you need
conda create --name e39 python=3.9 --channel conda-forge
Python 3.9 is available with later versions of conda. Use the below command:
conda create --name <myenv> python=3.9
And it will create your Python 3.9 virtual environment simply.
The only solution that works was creating a new Conda environment with the name you want (you will, unfortunately, delete the old one to keep the name). Then create a new environment with a new Python version and rerun your install.sh script with the Conda/pip installs (or the YAML file or whatever you use to keep your requirements):
conda remove --name original_name --all
conda create --name original_name python=3.8
sh install.sh # Or whatever you usually do to install dependencies
Doing conda install python=3.8 doesn't work for me. Also, why do you want 3.6? Move forward with the world ;)
Note the below doesn't work:
If you want to update the Conda version of your previous environment you can also do the following (more complicated than it should be because you cannot rename environments in Conda):
create a temporary new location for your current environment:
conda create --name temporary_env_name --clone original_env_name
delete the original env (so that the new env can have that name):
conda deactivate
conda remove --name original_env_name --all # Or its alias: `conda env remove --name original_env_name`
then create the new empty environment with the Python version you want and clone the original environment:
conda create --name original_env_name python=3.8 --clone temporary_env_name
Open an Anaconda PowerShell prompt as an administrator user.
Type in conda update python.
Wait about 10 minutes. In this process, you may need to type in y at some time.
After completing, check your Python version in Conda by typing python --version
If it is the newest version, then you can restart your computer.
The best method I found:
source activate old_env
conda env export > old_env.yml
Then process it with something like this:
with open('old_env.yml', 'r') as fin, open('new_env.yml', 'w') as fout:
for line in fin:
if 'py35' in line: # replace by the version you want to supersede
line = line[:line.rfind('=')] + '\n'
fout.write(line)
Then edit manually the first (name: ...) and last line (prefix: ...) to reflect your new environment name and run:
conda env create -f new_env.yml
You might need to remove or change manually the version pin of a few packages for which which the pinned version from old_env is found incompatible or missing for the new python version.
I wish there was a built-in, easier way...

Categories

Resources