having default Mac Python 2.7 and Anaconda Python 3 - python

I want to keep the Mac Python as my main 'python'. The reason for that is the recommendation in Python website here. I also want to add a separate environment for my Python3 (Anaconda).
For doing that I installed the Anaconda Python to get access to Conda and then I made an environment for my Python3 using the following command:
conda create -n py36 python=3.6 anaconda
When I installed the Anaconda python it added this to my .bash_profile file to get access to all conda commands:
# added by Anaconda3 4.4.0 installer
# export PATH="/Users/omidb/anaconda/bin:$PATH"
Now my default python is anaconda python which I don't want to.
How can I have default Mac python as my main python and then when I needed Anaconda, just use source activate py36 ?

UPDATED ANSWER
After testing this, I feel it's appropriate to offer this as a simple solution for using Mac Python as the default and only using Conda Python when desired.
You need to add/move the conda path to the end of your PATH environment via export command. This should allow you to use the Mac Python as the default and only use Anaconda Python after calling source activate py36.
export PATH="$PATH:/Users/omidb/anaconda/bin"
Path Resolution
This solution assumes you have /usr/bin/ (where Mac Python is) already in your PATH. Resolution order should check that directory first assuming it's first in the PATH. Also, this setup does not require symlinks in /usr/local/bin. I am not a fan of manipulating system-level resources for solutions that can be done with user resources (directories).
Default Python Setup
After moving the Anaconda path to the end of your PATH environment variable, you can validate that which python references /usr/bin/python, the location for Mac Python. You will run Mac python by default at the command line.
Running Conda Python
As previously noted, you have to call source activate py36 when you want to use the conda virtual environment. There is no need for adding symlinks to /usr/local/bin as they are already available through ~/anaconda/bin/.
Furthermore, source activate py36 (or any other Anaconda environment), it will add the appropriate environment path for Anaconda python to the beginning of your PATH environment variable, which (referring back to Path Resolution) would be executed when run as python on the command line. You can validate this with which python after running source activate py36. conda also stores the previous path as the environment variable CONDA_PATH_BACKUP.
Deactivating Conda
After running source deactivate, the original path is restored, so you will then be back to running Mac python.

Faced the same problem and question is too old but the simplest of doing this which I found is:
1. Let's check if the default python version is pointing to Conda python
which python - If Conda installation updated to your bashrc or zshrc, it will show that path
Running Command:
conda config --set auto_activate_base false
It's will make sure that when you start the terminal, Conda doesn't get activated as base
Now if you check python --version or which python - It should be pointing to mac default python version
Now whenever you want to use conda, all conda commands are available with conda <command>
Create the virtual env using conda create --name venv and activate it using conda activate <venv_name>
Now, I am able to use different python versions I required with conda and default python version as system default

Related

Why isn't Anaconda working inside of one of my Conda environments while using Ubuntu LTS?

I'm not sure why I can't get Anaconda to launch while I'm inside of one of my Conda environments; I'm using Ubuntu LTS. While I'm in the base environment, I can run the following command in my terminal and Anaconda will start up just fine.
anaconda-navigator
I also set up a Conda environment with specifications for a specific version of python and specific versions of libraries in a .yml file; conda was able to download and install everything successfully. When I run the below command in my terminal, I can see that the environment changes.
conda active {environment name}
My problem is that when I try to start up anaconda while inside of this new environment I get a command not found message.
How do I fix this error?
This is the intended behavior - anaconda-navigator is only meant to be launched from base environment. When base is not active, only the condabin/ directory from the base environment is still retained on $PATH. If you really want to launch Anaconda Navigator from other environments, you could try creating a symlink/softlink pointing at the base bin/anaconda-navigator, e.g.,
conda activate base
cd $CONDA_PREFIX
ln -s bin/anaconda-navigator condabin/anaconda-navigator
However, this is non-standard and may lead to unexpected behavior.
Otherwise, you would need to install anaconda-navigator in each environment, which I don't think is recommended.

how to fix error when importing geopandas in Spyder, possibly due to qgis install? [duplicate]

I have been using Spyder installed with with Anaconda distribution which uses Python 2.7 as default. Currently I need to set up a development virtual environment with Python 3.4.
Top two suggestions after research online are:
to set up virtual environment first and to point change the preferences of Spyder , e.g here;
to install all Spyder dependencies, like PyQt4, in the virtual environment itself, e. g. here ;
Both recommendations are cumbersome and do not look like smart options for development.
Is there a solution that would allow to run Spyder with required Python version automatically after activating the required virtual environment?
Here is a quick way to do it in 2021 using the Anaconda Navigator. This is the most reliable way to do it, unless you want to create environments programmatically which I don't think is the case for most users:
Open Anaconda Navigator.
Click on Environments > Create and give a name to your environment. Be sure to change Python/R Kernel version if needed.
Go "Home" and click on "Install" under the Spyder box.
Click "Launch/Run"
There are still a few minor bugs when setting up your environment, most of them should be solved by restarting the Navigator.
If you find a bug, please help us posting it in the Anaconda Issues bug-tracker too! If you run into trouble creating the environment or if the environment was not correctly created you can double check what got installed: Clicking the "Environments" opens a management window showing installed packages. Search and select Spyder-related packages and then click on "Apply" to install them.
There is an option to create virtual environments in Anaconda with required Python version.
conda create -n myenv python=3.4
To activate it :
source activate myenv # (in linux, you can use . as a shortcut for "source")
activate myenv # (in windows - note that you should be in your c:\anaconda2 directory)
UPDATE. I have tested it with Ubuntu 18.04. Now you have to install spyder additionally for the new environment with this command (after the activation of the environment with the command above):
conda install spyder
(I have also tested the installation with pip, but for Python 3.4 or older versions, it breaks with the library dependencies error that requires manual installation.)
And now to run Spyder with Python 3.4 just type:
spyder
EDIT from a reader:
For a normal opening, use "Anaconda Prompt" > activate myenv > spyder (then the "Anaconda Prompt" must stay open, you cannot use it for other commands, and a force-close will shut down Spyder). This is of course faster than the long load of "Anaconda Navigator" > switch environment > launch Spyder (#adelriosantiago's answer).
What worked for me :
run spyder from the environment (after source activate)
go to Tools --> preferences --> python Interpreter and select the python file from the env you want to link to spyder
ex : /home/you/anaconda3/envs/your_env/bin/python
Worked on ubuntu 16, spyder3, python3.6.
Additional to tomaskazemekas's answer: you should install spyder in that virtual environment by:
conda install -n myenv spyder
(on Windows, for Linux or MacOS, you can search for similar commands)
To do without reinstalling spyder in all environments follow official reference here.
In summary (tested with conda):
Spyder should be installed in the base environment
From the system prompt:
Create an new environment. Note that depending on how you create it (conda, virtualenv) the environment folder will be located at different place on your system)
Activate the environment (e.g., conda activate [yourEnvName])
Install spyder-kernels inside the environment (e.g., conda install spyder-kernels)
Find and copy the path for the python executable inside the environment. Finding this path can be done using from the prompt this command python -c "import sys; print(sys.executable)"
Deactivate the environment (i.e., return to base conda deactivate)
run spyder (spyder3)
Finally in spyder Tool menu go to
Preferences > Python Interpreter > Use the following interpreter and paste the environment python executable path
Restart the ipython console
PS: in spyder you should see at the bottom something like this
Voila
I just had the same problem trying to get Spyder to run in Virtual Environment.
The solution is simple:
Activate your virtual environment.
Then pip install Spyder and its dependencies (PyQt5) in your virtual environment.
Then launch Spyder3 from your virtual environment CLI.
It works fine for me now.
The above answers are correct but I calling spyder within my virtualenv would still use my PATH to look up the version of spyder in my default anaconda env. I found this answer which gave the following workaround:
source activate my_env # activate your target env with spyder installed
conda info -e # look up the directory of your conda env
find /path/to/my/env -name spyder # search for the spyder executable in your env
/path/to/my/env/then/to/spyder # run that executable directly
I chose this over modifying PATH or adding a link to the executable at a higher priority in PATH since I felt this was less likely to break other programs. However, I did add an alias to the executable in ~/.bash_aliases.
From Spyder official page on Github:
The naive approach
To use Spyder with another environment, the simplest way is to just
install it directly into the environment from which you'd like to use
the packages in, and run it from there. This works with all Spyder
versions and should require no extra configuration once the IDE is
installed; however, it results in multiple installations to manage and
isn't as flexible or configurable as the alternative. Therefore, when
dealing with multiple environments, we recommend the modular
approach.
The modular approach
Starting with Spyder 3.3.1, you can install the modular
spyder-kernels package into any Python environment (conda
environment, virtualenv/venv, system Python, WinPython, etc) in
which you wish to work, and then change the Python interpreter used by
Spyder on its IPython consoles to point to the Python executable of
that environment.
This takes a small amount of preparation and configuration, but is
much "lighter" and quicker than a full Spyder installation into that
environment, avoids dependency conflicts, and opens up new workflow
possibilities.
To achieve this, follow these steps:
1- Activate the environment (e.g. myenv) in which you'd like to work (e.g. with conda activate myenv for conda, source myenv/bin/activate or workon myenv for virtualenv/venv, etc)
2- Install the spyder-kernels package there, with the command:
3- conda install spyder-kernels if using conda/Anaconda,
4- pip install spyder-kernels if using pip/virtualenv.
5- After installing via either method, run the following command inside the same environment:
python -c "import sys; print(sys.executable)"
and copy the path returned by that command (it should end in
python, pythonw, python.exe or pythonw.exe, depending on your
operating system).
6- Deactivate that environment, activate the one in which Spyder is installed (if you've installed it in its own environment) and start
Spyder as you normally would.
7- After Spyder has started, navigate to Preferences > Python Interpreter > Use the following interpreter and paste the path from
Step 3 into the text box.
8- Start a new IPython console. All packages installed in your myenv environment should be available there. If conda is used, the
name of the current environment and its Python version should be
displayed in Spyder's status bar, and hovering over it should display
the path of the selected interpreter.
On Windows:
You can create a shortcut executing
Anaconda3\pythonw.exe Anaconda3\cwp.py Anaconda3\envs\<your_env> Anaconda3\envs\<your env>\pythonw.exe Anaconda3\envs\<your_env>\Scripts\spyder-script.py
However, if you started spyder from your venv inside Anaconda shell, it creates this shortcut for you automatically in the Windows menu. The steps:
install spyder in your venv using the methods mentioned in the other answers here.
(in anaconda:) activate testenv; though in my case, this step was not needed.
Look up the windows menu "recently added" or just search for "spyder" in the windows menu, find spyder (testenv) and
[add that to taskbar] and / or
[look up the file source location] and copy that to your desktop, e.g. from C:\Users\USER\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Anaconda3 (64-bit), where the spyder links for any of my environments can be found.
Now you can directly start spyder from a shortcut without the need to open anaconda prompt.
For me below worked:
Open Anaconda, setup new environment, then select the env and click on play icon as shown below:
Then click on Open Terminal, and type "spyder" in terminal, it will open the spyder with selected env.
Note: For me directly opening Sypder from Home page was always opening with base env.
I follow one of the advice above and indeed it works. In summary while you download Anaconda on Ubuntu using the advice given above can help you to 'create' environments. The default when you download Spyder in my case is: (base) smith#ubuntu ~$. After you create the environment, i.e. fenics and activate it with $ conda activate fenics the prompt change to (fenics) smith#ubuntu ~$. Then you launch Spyder from this prompt, i.e $ spyder and your system open the Spyder IDE, and you can write fenics code on it. Remember every time you open a terminal your system open the default prompt. You have to activate your environment where your package is and the prompt change to it i.e. (fenics).

make virtualenv with specific python version(MACOS)

I installed brew, python3 (default and latest version) and pip3, pyenv.
TensorFlow does not support python3.7 now, so I heard that I should make a virtualenv that runs 3.6 or lower version independently.
I installed python 3.6.7 by pyenv install 3.6.7 but can't make virtualenv -p 3.6.7 (mydir) because 3.6.7 is not in the PATH (usr/local/bin).
How can I update my PATH?
You don't need the executable to be on the PATH. Assuming you want /usr/local/bin/python3.6.7 to be used in the virtual environment,
virtualenv -p /usr/local/bin/python3.6.7 mydir
Updating your PATH is easy:
PATH=/usr/local/bin:$PATH
This will only update it in your current session; you might want to add this to your shell's startup files to make it permanent. This is a common FAQ but depends on a number of factors (your shell, etc) so google for details. Here is one question with several popular variants in the answers: Setting PATH environment variable in OSX permanently
I know that this doesn't answer the question exactly, but for completeness I'd like to add an Anaconda solution. Provided that an Anaconda environment is present on the system, a new Python environment can be created using conda create -n py36 python=3.6 pip. The name py36 can be arbitrarily chosen (could also be e.g. myenv or tensorflow), the desired Python version (in this example 3.6) is specified by python=3.6.
This environment can then be activated using conda activate py36 (or whatever name you assigned in the previous step). Once the environment is active, you can install tensorflow via pip: pip install tensorflow-gpu. To deactivate the current environment and return to the default environment, use conda deactivate. In this way, you don't have to modify PATH variables.
See also this documentation page for more details on the Anaconda environment.

iPython with different env (using anaconda)

I have just created a new env with python 3.5 using anaconda (called it python35). My root env points to python 2.7.11. I cant seem to launch ipython with this new env, here is what I did
1. in conda prompt, activate required env: activate python35
2. confirm the version: python --version
3. launch ipython: ipython notebook
4. open a notebook and do: import sys; print (sys.version)
Step #2, returns 3.5 but step #4 always gives me 2.7.11, its like ipython is picking up python version from root env. How do I fix this. Thanks for any help!
This question is related but I have already done what it suggests.
AFAIK, different environments in anaconda (and in venv as well) are activated by prepending env path to $PATH environment variable. It means, that if some file (eg, ipython) is not found in env path (the first entry of $PATH), the system searches for it in consequent entries of $PATH and finds it in root environment (that stays in $PATH). To fix the behavior, you need to install its own copy of ipython to anaconda env:
In command prompt, activate the environment: source activate python35 (or simply activate python35, depending on the OS)
While in environment, issue the command conda install ipython-notebook
An addition to Andrey Sobolev solution,you should switch to conda install ipython for higher python3.x version and log out the environment by Ctrl+D or conda deactivate then activate again
I could confirm the solution above (basically install notebook in active environment) in my case. Two updates from my side:
Since Anaconda 4.4 (?) ipython notebook is now jupyter notebook. So I had to install jupyter instead. I guess, deactivate and activate was required afterwards to get the path variables in notebook updated -> checked by python -c "import sys; print(sys.path)" or corresponding command in notebook.
If you aren't using a isolated anaconda environment, you may eventual calling the notebook package from your native OS-Python installation, instead the desired from Anaconda. Similarly, I had trouble with cmake or cxx, when I try to compile in Anaconda Environment - the diffent root folder might found in linux bash with e.g "which jupyter"

Installing Anaconda into a Virtual Environment

I've currently got a working installation of the Enthought Python Distribution on my machine that I don't want to necessarily disrupt, but I'd like to look at moving over to Anaconda from Continuum.
I can easily install Anaconda into the virtualenv directory I create, but I'm not sure how to tell that virtualenv to use the anaconda-version of Python. If I was telling my whole system to use it I can alter .bash_profile with something like export PATH="/DIRECTORIES/anaconda/bin:$PATH. Is there a way to do that within a virtualenv?
I just tested the Anaconde 1.6 installer from http://continuum.io/downloads
After downloading, I did:
bash Anaconda-1.6.0-Linux-x86_64.sh
If you take the defaults, you'll end up with a directory anaconda in your home directory, completely separate from your EPD or system Python installation.
To activate the anaconda installation's default environment, do the following:
source $HOME/anaconda/bin/activate ~/anaconda
All Python commands will now come from the default Anaconda environment in $HOME/anaconda, which is itself a kind of a virtual environment. You can create sub-environments with e.g. conda create -n myenv1 ipython scipy, but this is not necessary.
As a side note, you can also use pip (also in $HOME/anaconda/bin) to install PyPI packages into your Anaconda default environment (it has pip installed by default) or any of the sub-environments (in which case you should first install pip into the sub-environment using conda install -n myenv1 pip).
It is possible to install parts of Anaconda manually into an existing virtualenv, but using their installer is by far the easiest way to test and use, without affecting any of your existing Python installations.
When you create your virtualenv use the -p flag to give it the path to the Python executable you want to use:
virtualenv -p /path/to/python-anaconda-version

Categories