how to prioritize default mac python environment over miniconda - python

I installed miniconda for some software I need to run. It worked great, but it made all of the other web related stuff I have set up through mac's default python environment stop working. What I would like to have is the mac python environment as the default and conda only when I need to run this specific software. So I would need to know #1 - how do I modify .bash_profile to allow me to run both environments, and #2 - the command I need to switch between environments (if there is one).
My bash profile looks like:
# Setting PATH for Python 3.6
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.6/bin:${PATH}"
export PATH
# added by Miniconda3 4.3.21 installer
# export PATH="/Users/mikeheavers/miniconda3/bin:$PATH"
(I have commented out the conda path for now)
Thanks!

Have you considered using Python's Virtual env?
This allows you to have a completely separate Python installations without causing conflicts with your main python in your path. This sounds ideal for your development needs.
You would need to "activate" the virtualenv prior to starting up miniconda, which will adjust your environmental variables such that the virtualenv python, and it's libraries will be used.
(copying from the link)
This will result in a virtual python installation
$ pip install virtualenv
$ cd my_project_folder
$ virtualenv my_project
$ source my_project/bin/activate
$ # Do stuff, like install from pip
$ deactivate # This will turn off the virtual python in your path
you can use this interpreter in your bashrc too (Check out the link for a more in depth introduction)
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python2.7

Related

Is it possible to have two distinct install of Python 3 of the same revision on a Windows system?

I know it possible to have two installs of Python of different versions on a Windows system. But I cannot manage to have two installs of the same revision (in my case 3.8.10) to coexist.
I'm designing an application that creates a Python process. That process needs to run from a specific version of Python with packages of specific versions installed on it. In order to fully control the Python install, decision was made to install it inside the application distribution directory, segregating it from any other Python installed on the system. No environment variable refers to it.
As part of the the deployment/install process for the application, a PowerShell script downloads the Python installer and installs Python and the necessary packages into the application distribution directory. The Python installer is invoked as follows:
.\\python-3.8.10-amd64.exe /quiet InstallAllUsers=1 PrependPath=1 Include_test=0 TargetDir="$curDir\\Python" Include_exe=1 Include_lib=1 Include_pip=1 Include_tcltk=1 | Out-Null
It works well unless the system has already a Python install of the same version installed on it. In that case, running the installer will break the existing install, and not fully install the new one.
I tried to run the installer manually and I noticed that it is able, somehow, to detect that an install of the same revision exist on the system. In that case, it does not allow an new install. To do so, I would have to uninstall Python at its current location to be able to install it somewhere else.
Is there a way to have two distinct installs of Python 3 of the same revision on a Windows system? And if yes, how can it be done?
A better aproach instead of installing python again would be using virtual environments.
To create a new python env. Open the command line (Powershell) on Windows and navigate to the directory you want your python env to be.
Type python3 -m venv tutorial-env. This will create a new python virtual env named tutorial-env
To activate that env on Windows powershell type: tutorial-env\Scripts\activate.bat
To deactivate the env type deactivate
If you are wondering what python virtual environments do. They basically do what you are trying to do but without installing python globally again. When you create a new python env, a new python3 is placed in your env directory, in this case in the tutorial-env directory, and when you activate the environment, it replaces the python global path to the path in your env (in this case in tutorial-env). Now when you are on this virtual env and install new python packages, they will only be available when you activate that env.
For more information about virtual environments please refer to Python official docs.

Do I need to activate virtual environment when using venv

Using Python 3.7.0 on Mac. Trying to use venv module that was added post python 3.4.
I setup my virtual env using python3 venv -m path/to/my/dir - my question is do I need to activate this virtual env to use?
The documentation seem to imply I don't need to?
You don’t specifically need to activate an environment; activation just prepends the virtual environment’s binary directory to your path, so that “python” invokes the virtual environment’s Python interpreter and you can run installed scripts without having to use their full path. However, all scripts installed in a virtual environment should be runnable without activating it, and run with the virtual environment’s Python automatically.
If I don't have to activate, what is the benefit of prepending venv to binary directory? Wouldn't this have to happen regardless for a venv to work?
Activating the virtualenv gives you convenience. It is never required.
Even for scripts that are configured to run with #!/usr/bin/env python, (which looks up the python executable on your path), you could manually update the PATH environment variable:
$ PATH="/path/to/venv/bin" some_script
Activating makes the PATH update stick until you deactivate again, and that can be convenient.
For example, I regularly have several virtualenvs in use at any one time. Some of them are there only to install some command-line tools I have symlinked into my ~/bin/ directory, another is home to a Jupyter notebook, and 2 more are used to quickly verify code snippets in different Python versions with access to 3rd-party libraries. I don't activate any of those.
When you don’t activate a virtualenv, all that happens is that your terminal PATH variable is not updated to put the bin directory of the virtualenv first, so when you enter python or pip or other script without any path into the terminal, the shell will find a different Python binary to run. You can always use any of the commands in the virtualenv bin/ directory by giving the full path to that command.

Anaconda Python: where are the virtual environments stored?

I am new to Anaconda Python and I am setting up a project in Sublime Text 3. I have installed Anaconda and created a virtual environment using:
conda create -n python27 python=2.7 anaconda
conda create -n python35 python=3.5 anaconda
I am having trouble setting up the Virtualenvs plugin for SublimeText 3.
When I try, it asks me for a virtualenvs path which I give:
~/users/../anaconda/envs/python27
Then it asks for what I'm assuming is a path to a python distribution because it lists file paths for the system versions of python -- but not the anaconda install.
I have no real desire to use the plug in, I just want to be able to use both versions of python. Could I use a project settings file to set the version of python instead?
If you activate the environment you're interested in, you can find that answer in the environment variables.
on MacOS/Linux:
source activate python35
echo $CONDA_PREFIX
on Windows:
conda activate python35
echo %CONDA_PREFIX%
You can also run conda info --envs, and that will show the paths to all your environments.
To get the path to the instance of python being used by a particular environment, do the following:
on MacOS/Linux:
source activate python35
which python
on Windows:
conda activate python35
where python
That should return the path you're looking for.
You can run the command conda info.
This will output something like this:
envs directories : C:\Users\Geo\.local\Miniconda3\envs
C:\Users\Geo\.conda\envs
C:\Users\Geo\AppData\Local\conda\conda\envs
I have installed conda at C:\Users\Geo\.local\Miniconda3.
Then with the command conda info -e you get the location of each environment.
(base) C:\Users\Geo>conda info -e
# conda environments:
#
miniconda2 C:\Users\Geo\.conda\envs\miniconda2
base * C:\Users\Geo\.local\Miniconda3
anaconda3 C:\Users\Geo\.local\Miniconda3\envs\anaconda3
ml C:\Users\Geo\.local\Miniconda3\envs\ml
Your environments are located in Anaconda3\envs\<yourEnv_directory>\
To answer your question the folder for your python binaries and packages for the environment are located in ~Anaconda_installation_folder~/envs/python35.
But I cannot really say if that solves your problem. Normally you just switch to your environment source activate python35 and then type python. This will automatically give you the "right" python executable. So if you have a package you could use:
source activate python35
python setup.py install
# Now it is installed in your python35 environment
source activate python27
python setup.py install
# Now it is also installed in your python27 environment
Just change python setup.py install to what you want to do in the environment. I don't have any experience using Sublime Text and what you mean with build system. But you can always use something like tox which automates a lot of these manual builds.
None of the other windows solutions worked for me so I'm providing my own. Activate the environment inside anaconda prompt, then issue the command 'where python' and you'll likely see multiple results but one of them, most likely the top one, is the one you're after. For me, my environments were located in AppData\Local... which is not what anyone else had mentioned but the best solution is to use 'where python' which should result in an answer regardless of how you've installed Anaconda.
For me, with default anaconda settings and Windows 10, the path that displays after activating the environment is C:\Users\usrname>, but it does not contain an Anaconda3 folder. However, it contains a .conda folder that contains an environments.txt file that lists all conda environments and their locations. By default, the environment folders were stored in:
C:\Users\usrname\AppData\Local\conda\conda\envs\EnvName
None of the above worked. In the end, I found mine at:
c:\Users\myusername\venvs\test1-V7fphpR9\
Open your c directory
Go to Users and then Open your naming folder (in my case Suman Biswas)
Fiend Anaconda3 folder
Fiend the folder envs in Anaconda3
Now you can see your virtual environment
enter image description here
enter image description here
enter image description here
On Windows 10 x64 and Anaconda3, the python interpreter for a newly created environment "my_env" would appear here:
C:\ProgramData\Anaconda3\envs\my_env\python.exe
Or here:
C:\Users\[username]\AppData\Local\conda\conda\envs\my_env
Check both places.
Update 2020-07-17
If Anaconda is installed as Administrator, then the default is one location.
If Anaconda is not installed as Administrator, then it appears in a different location.

Pyenv not auto activating

I have pyenv installed in my environment and up to this weekend (when I installed 'Kivy') my pyenv/local setup has been working fine. But now when I go to my various python project directories, pyenv does not automatically activate the right python version properly.
E.g.
I create an environment using pyenv like this,
pyenv virtualenv 3.3.2 work
I make and go into a dir called work and have a .python-version file with the text work as the sole content.
Pyenv detects that my environment is work using this file but my python version is not python 3.3.2 instead it's 2.7.9.
For some reason, something happened, and all of my pyenv virtual environments use 2.7.9 as opposed to the python version they were created with.
When I run which python I get,
/opt/boxen/homebrew/bin/python
when I go to the pyenv version directory and run
$ cat pyvenv.cfg
home = /opt/boxen/pyenv/versions/3.3.2/bin
include-system-site-packages = false
version = 3.3.2
However, if I run pyenv activate I my python version switches to python 3.3.2 (or the appropriate version for a given env).
Question is, how do I get pyenv to auto activate the environment's python version as it did before (before I did something to break it).
It sounds like, because of which python not saying it's the shim, you don't have the bin/shims path first in your PATH envvar. Add these lines to your shell startup script, and make sure they're at the end, after any other path manipulations.
export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
The eval line does some additional shell monkeying I think to add the .pyenv/shims directory...check that with an echo $PATH maybe.

activated virtual environment doesn't work

I have virtual env called env1
I'm activating it via: source env1/bin/activate.
It does seem to be activated however if I simply run:
python
I can't import any packages I have in my virtual env. This seems to be the case with every virtual environment I have on this machine.
What's wrong with my system? or am I doing something wrong myself?
Its hard to tell just by your description but:
Activated environment will (in most linux distros) prefix your shell prompt with the name of environment. Like:
username#computer ~ $ source env1/bin/activate
(env1)username#computer ~ $ python
That should run you python from env/bin/python
Be sure that you have installed all packages you want included. Virtual environment can also be set to include or omit system packages. It is easily set during creation of virtualenv by including --system-site-packages argument.
See: http://virtualenv.readthedocs.org/en/latest/virtualenv.html#the-system-site-packages-option

Categories