How to install multiple Python versions on Mac M1 - python

I already installed Python 3.9.2 as it supports ARM64 as recommended in Python.org
I created a virtual environment after that using python3 -m venv py39
Now I need to have another environment but with Python 3.8.8 as Tensorflow is supporting 3.8 only.
How could I create another virtual environment with Python 3.8 while maintaining the other 3.9 env.
In case of asking me to use conda, Does conda support Mac M1 ARM64 as it doesn't according to my search
I found same question asked many times but for windows and answers are very old like:
Use different Python version with virtualenv 11 Years ago

Is there a reason you're tied to conda?
The reason I ask is there are easier routes to getting isolated Python environments, which might have moved a bit faster on the needed M1 migrations.
I recommend at least checking out
Pyenv
pipenv
poetry
If you are tied to conda, it seems like there are some related posts: How can I run Python 3.9.1 natively on M1 Mac?

i baught M1 and started struggling with packages and setup M1 is really fast but i was having too much issues one of the issue is python environments on intel it was very easy finally i over come to the problem with python using miniforge
here are the steps i followed
brew install miniforge
Then init zsh shell using conda
conda init zsh
and finally instantiate required python env below command will install all the dependencies
conda create -n .venv python=3.7.6
then lastly activate the environment
conda activate .venv

I was having issues after following above solutions but it didn't work for me
bash-5.1$ conda activate .venv
CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
To initialize your shell, run
$ conda init <SHELL_NAME>
Currently supported shells are:
- bash
- fish
- tcsh
- xonsh
- zsh
- powershell
See 'conda init --help' for more information and options.
IMPORTANT: You may need to close and restart your shell after running 'conda init'.
i have zsh so i switched to bash shell and everything was started working for me

I found out, with pyenv and miniforge it is no problem to work with conda, even with older versions like 3.8, which seemed to be a constrained on M1. With
brew update
brew install pyenv
pyenv install miniforge3-4.10
pyenv global miniforge3-4.10
conda create -n new_env python=3.8
conda activate new_env
conda install poetry
poetry new project
cd project
poetry add open3d
With this you can install any package and can use conda in every way you want, as far as I tested it.
Instead of peotry you can of course use conda install {package} in the usual way.

Related

How to activate virtual environment while working on django project?

I have been through numerous similar questions and I still don't understand how are we activating virtual environment for Django projects. Please mention explanation for how each command works and one more questions is that why do we not need to install python in Django's virtual environment, I am getting confused. Thank you in advance, please help this newbie.
Benefits
You can use any version of python you want for a specific environment
without having to worry about collisions (shoutout to my python 2.7
mac users!)
You can organize your packages much better and know exactly the
packages you need to run your code incase someone else needs to run
it on their machine
Your main python package directory does not get FLOODED with
unnecessary python packages
To create virtual environment
step 1 install environment package (virtualenv) using pip
pip install virtualenv
step 2 create virtualenv
virtualenv env_name #<- env_name is virtualenv name you can set any
step 3 Activate Virtual env
env_name\Scripts\activate #<- for window
step 4 Install pakages you wan to install in virtual env
cmd(env_name): pip install django
Note that python is install in your virtual env automaticaty the
version is same as in your local machine
There is no difference between activating virtual environment for Django or for other purposes. Django on it's own does not differ from any Python library out there.
Python virtual environment allows you to separate your system Python and it's libraries and create self-contained directory tree that contains a Python installation for a particular version of Python, plus a number of additional packages.
On Linux, assuming you already have Python 3 and pip3 installed:
# install virtualenv package (skip if you have it already)
pip3 install virtualenv
# create virtual environment in directory "tutorial-env"
python3 -m venv tutorial-env
# activate virtual environment
source tutorial-env/bin/activate
Upon activation below command should give path to new Python binary:
which python3
Similarly with pip3
which pip3
As long as your environment is activate you can run pip3 install $package_name and it will install it inside virtual environment.
To deactivate your virtual environment:
deactivate
For more info and commands for Windows:
https://packaging.python.org/en/latest/guides/installing-using-pip-and-virtual-environments/
https://docs.python.org/3/tutorial/venv.html

How to create a Python 3 environment that has only standard lib, no extra packages

I'm trying to test how a package that I've created installs on a machine with a base Python 3 install, meaning no additional packages have been installed via pip, easy_install, etc.
How do I setup a virtual environment that acts like a clean install of Python 3? If I create a pipenv or virtualenv, then list all my installed packages, the 30 or so that I have are all still there. How can I make a Python environment that behaves like a brand new install of Python and has no extra packages?
This is in Raspberry Pi OS which has only one user, so using the --user flag in pip install --user might as well be a global install. I considered creating a new pipenv then pip uninstalling everything, but am worried that will break something.
Thanks for your help.
With virtualenv you would do :
virtualenv myenv -p python3.8
This would create a virtual environment in the myenv directory with python3.8 and the standard library only. You would activate this by doing
source myenv/bin/activate.sh
If you are using virtualenvwrapper, then you create the virtual environment by doing:
mkvirtualenv myenv -p python3.8
and activate it by doing:
workon myenv
I am sorry I am not a virtualenv user, however here is what I could suggest if you have conda installed:
conda create -n myenv python=3.6
Where myenv is the name of your environment and 3.6 the python version.
You can then enable the environment by doing:
conda activate myenv
A pipenv solution to this would be to create the virtual environment by specifying the version of python you wish to use.
pipenv --python <version>
This will create a virtual environment with the specified version of python and nothing else.
e.g.
pipenv --python 3 # Will use the first python 3 version found on your machine
pipenv --python 3.9 # Will use python 3.9 if it is installed on your machine
This works with --python 2 and --python 2.x also.
https://pipenv-fork.readthedocs.io/en/latest/basics.html#specifying-versions-of-python

Install different versions of python 2

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.

Trouble using pyenv and anaconda environments

I'm using pyenv to manage my python environments. One of the environments is anaconda with multiple environments.
There are two ways to activate a conda environment while everything is handled by pyenv.
Load the conda environment directly with pyenv as pyenv local anaconda3-2019.10/env/myenv
Load the main conda and then activate the local environment pyenv local anaconda3-2019.10; conda activate myenv
Both methods fail one way or another:
In the first method, conda command cannot be used to install any more modules as it says command not found
In the second method, the conda activate fails, complaining about the shell: CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'. Configuring the shell ruins all the environments by changing .zshrc which is global
I can use pip to install packages but the whole point of using anaconda is to use the nice package managing environment
Have I set something wrong or is this some sort of bug?

How do I install a package for different Python versions in Anaconda?

I have Python 2.7 as root. I need to install the package "statistics" in Python 3.6, and it is not in the environments of anaconda navigator. How can install "statistics" with conda or pip for a secondary Python environment?
Create a new Python 3 environment by running:
conda create --name python3 python=3
If you want all the standard anaconda packages installed by default, do:
conda create --name python3 python=3 anaconda
Whenever you need to use python3 run:
activate python3
Then use the command line as normal. So, if you want to install something into your python3 environment, make sure you activate python3 first.
Note that python 3 has it's own statistics module that you may find useful, and this module has been ported to python 2 if you would prefer.

Categories

Resources