My system is ubuntu 18.04.
I have a pre-installed version 3 and 2 of python.
which python3
/usr/bin/python3
python3 -V
Python 3.6.9
which python
/usr/bin/python
python -V
Python 2.7.17
I need to create several virtual environments, one for python 2.7.15 and another for 2.6. how can I do that?
There are different ways of creating virtual python environments. Three popular ones are
virtualenv
pipenv
conda
I personally like conda a lot.
virtualenv
Assuming you have pip installed, you get virtualenv with
pip install virtualenv
Once installed, you can change into a directory of your choice and create a virtual environment like this
virtualenv myenvironmentname
If you want to use a different python version in your virtual environment, you can specify this with the --python flag.
virtualenv --python=/usr/bin/python2.6 myenvironmentname
However, please note that this requires you to have the python version you specify installed in advance, virtualenv will not take care of that for you (have a look at Use different Python version with virtualenv for more details). So you'll need local installations of the versions you desire.
You then can activate the environment with
myenvironmentname/bin/activate
and go ahead to use pip to install packages, etc. Have a look at
pip freeze --help
to find out on how to make your environment reusable.
pipenv
pipenv combines pip and virtualenv.
You can install it using
pip install --user pipenv
Pipenv takes care of dependencies on a project basis
cd myprojectfolder
pipenv install
This will create a Pipfile which will track dependencies and a virtualenv (see https://docs.python-guide.org/dev/virtualenvs/ for more details).
To create an environment using a specific version, you can do
pipenv install --python '/usr/bin/python2.6'
or
pipenv install --python 2.6
Cmp. Set python version when creating virtualenv using pipenv. If you also have pyenv installed, the second form will prompt pipenv to attempt to install non-existing versions, afaik.
conda
Anaconda Python is a python distribution (with a focus on data science) that comes with its own package and virtual environment manager named conda. Anaconda Python is not available in the official package repository of Ubuntu 18.04 LTS but needs to be installed in another way (the official documentation can be found here: https://docs.anaconda.com/anaconda/install/linux/).
To create an environment with conda, do
conda create --name myenvironmentname python=2.7.15
In contrast to virtualenv, the environments are by default not created in the present working directory, but installed into the envs directory in your conda directory. conda will also take care to install the proper python version, that is at least as long as it is part of the default channel (see below).
You can then activate said environment with
conda activate myenvironmentname
As I wrote above, the python version you specify needs to be available from the configured conda channels. python2.6 however, was removed from the default channel. To remedy this, you can add the free channel back to your default list (see https://docs.conda.io/projects/conda/en/latest/user-guide/configuration/free-channel.html for more details):
conda config --set restore_free_channel true
After that you can
conda create --name myotherenvironmentname python=2.6
And switch between the environments as you like
conda activate myotherenvironmentname
For python3 python -m venv <your_virtual_enviroment_path> for python2 virtualenv <your_virutal_enviroment_path>
The to activate source <your_virtual_environment_path>/bin/activate. And to deactivate deactivate. Finally to check what is activated echo $VIRTUAL_ENV
I strongly recommend for one virtual environment for each project.
I want to create a fresh conda environment called new-gooey_env.
conda create --name new_gooey_env python=3.7
I get a notification that the following packages will be insatlled:
ca-certificates pkgs/main/win-64::ca-certificates-2020.6.24-0
certifi pkgs/main/win-64::certifi-2020.6.20-py37_0
openssl pkgs/main/win-64::openssl-1.1.1g-he774522_1
pip pkgs/main/win-64::pip-20.2.2-py37_0
python pkgs/main/win-64::python-3.7.7-h81c818b_4
setuptools pkgs/main/win-64::setuptools-49.4.0-py37_0
sqlite pkgs/main/win-64::sqlite-3.32.3-h2a8f88b_0
vc pkgs/main/win-64::vc-14.1-h0510ff6_4
vs2015_runtime pkgs/main/win-64::vs2015_runtime-14.16.27012-hf0eaf9b_3
wheel pkgs/main/win-64::wheel-0.34.2-py37_0
wincertstore pkgs/main/win-64::wincertstore-0.2-py37_0
zlib pkgs/main/win-64::zlib-1.2.11-h62dcd97_4
That's great. I install them and activate my new environment.
conda activate new_gooey_env
Now when I type pip freeze, I can see that basically every package on my computer has been installed into this new environment.
The output of pip freeeze in my new environment is about 100 packages.
How can I create a new environment with just the bare minimum packages?
I have already googled this problem and followed the instructions here, which doesn't work hence the question.
Note when I type conda list I only get the list of packages above.
So is pip freeze command correct? Is there also 100 packages in my new environment?
edit: On Windows.
Use pip freeze -l .
The l stands for local .
pip freeze otherwise acts on all your environments as your virtualenv has global access. That is my best guess.
And no , your new environment doesn't have 100's of packages!
I'm trying to install python 3.9 in a conda enviroment. I tried creating a new conda env using the following command,
conda create --name myenv python=3.9
But I got an error saying package not found because python 3.9 is not yet released
So, I manually created a folder in envs folder and tried to list all envs. But I couldn't get the manually created new environment.
So, how do I install python 3.9 in a conda env with all functionalities like pip working?
To create python 3.11 conda environment use the following command
conda create -n py311 python=3.11
py311 - environment name
Update 3
To create python 3.10 conda environment use the following command
conda create -n py310 python=3.10
py310 - environment name
Update 2
You can now directly create python 3.9 environment using the following command
conda create -n py39 python=3.9
py39 - environment name
Update 1
Python 3.9 is now available in conda-forge.
To download the tar file - https://anaconda.org/conda-forge/python/3.9.0/download/linux-64/python-3.9.0-h852b56e_0_cpython.tar.bz2
Anaconda Page - https://anaconda.org/conda-forge/python
As pointed out in the comments, python 3.9 is not yet there on any channels. So, it cannot be install yet via conda.
Instead, you can download the python 3.9 executable and install it.
Once the installation is done, a new executable will be created for python 3.9 and pip 3.9 will be created.
Python:
python3.7
python3.7-config
python3.7m
python3.7m-config
python3.9
python3.9-config
pip
pip
pip3
pip3.7
pip3.8
pip3.9
pipreqs
In order to install ipython for python 3.9,
pip3.9 install ipython
On 6-Oct-2020, Python 3.9 was made available on conda-forge: https://anaconda.org/conda-forge/python. However, most of the other packages (including some of the essentials to create a basic environment) didn't explicitly support Python 3.9 yet.
However (as of 15-Oct-2020), the basic dependencies appear to have been fixed and the following command now works:
conda create -c conda-forge python=3.9 -n py39-demo
You can now simply just run
conda create --name myenv python=3.9
And it will create your python 3.9 virtual environment simply.
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...
I've got a package that depends on an earlier version of Python -- I'm running 3.5.1, but the package only supports up through Python 3.4. I'd like to clone my current environment, decrement my Python version to 3.4, and then update all other packages to the latest version compatible with Python 3.4. How do I do that?
When I clone my root environment and attempt:
conda install python=3.4
I receive the message:
Error: 'conda' can only be installed into the root directory.
I'm working on Windows currently. Any ideas would be much appreciated.
Create a new environment:
conda create -n py34 python=3.4
activate it:
source activate py34
(no source on Windows)
Install all your packages:
(py34) conda install package1 package2
This keeps your original Python 3.5 install and you can activate Python 3.4 whenever you need it. Deactivate with deactivate.
You can export names and versions of all packages you have currently installed with:
conda list --export > list_of_my_packages
Use this file to create your new py34 environment:
conda create --file list_of_my_packages -n py34 python=3.4
You might get a warning about packages missing form the current channels.
Either add more channels or edit this list, opening the file in an editor.
Finally, you can update all packages to the latest version with:
conda update all