I have two pieces of code that require different versions of python and versions of packages. I have two conda environments that allow each piece of code to work separately. It seems impossible to create an environment that will support both of them. Is there a way to switch conda environment during the run (in python code), so that I could execute one part using one environment and the second part using the second environment in the same script? The form and format of the result of the first part is definitely supported by the second part, so I don't see a reason why it can't work.
I have had success using conda within a shell script that calls the python process. e.g. something along these lines
conda activate my_env_1
python some_process.py
conda deactivate
conda activate my_env_2
python some_process_2.py
conda deactivate
You will have to enable your shell to use conda. See this Python - Activate conda env through shell script
If you want to change the environments in the same code at the same time, unfortunately as far as I know that will be painful, or just impossible with one word. In order to fix this issue, just add the libraries that your other environment has into the one that you use mainly.
You can achieve this by opening either your cmd (commnand line prompt), or just anaconda prompt:
activate yourenvironment name
pip/pip3 install modulename
I am a new user to conda environment and was setting up to use TensorFlow , on Windows.
I came across a command -
source activate IntroToTensorFlow.
I understood IntroToTensorFlow is an environment we are creating, but does it mean we need to create this environment every time?. I am using jupyter notebook, so if I shutdown the kernel will the environment get deactivated?
And if I restart my PC, should I activate the environment everytime ?
Conda is a package manager that installs and manages (usually) Python libraries and (sometimes) non-Python packages. A conda environment is a sort of virtualenv virtual environment; its typical use case is to have a Python interpreter (any version) along with your choice of compatible Python libraries (any version).
The following example might most likely pertain to you. Suppose you have downloaded the implementation of a very nice paper implemented in TF and you want to try it out. But the authors implemented that when Tensorflow was just growing. The APIs have changed now, and so is the required CUDA version. You want to work ideally on the latest TF. Now, what do you do? An easy way to just try out this implementation is to create a different conda environment with the libraries needed for that implementation, run that in this environment, and perhaps if you like it, you might consider upgrading the TF APIs and use it in your code.
The conda environments are also pretty simple in its construction. If you installed conda using Anaconda and default options, you will have your environments in ~/anaconda3/envs. The environments are nothing but directories here, each having various configurations of Python interpreter and libraries of your choice. (So when you shutdown your PC/Jupyter, the environments will of course persist.) At the time of usage, you just switch between the environments to suit your needs. That is, when you source activate an environment, you will be allowed to use the Python interpreter and installed libraries from that environment. Note if you source deactivate or start a new terminal session, you will still be using the root environment.
Besides, Jupyter notebook, if setup with this plugin, will allow you to have nice integration with conda environments and you wouldn't even need to source activate everytime you want to switch. You can choose between the various settings (or conda environments), which are interpreted as different kernels in the notebook. So it would as simple as choosing some environment using a drop-down.
source activate IntroToTensorFlow does not create an environment, it simply activates an environment that has already been created. To create that environment (with tensorflow installed), use conda create -n IntroToTensorFlow tensorflow.
You do not need to create the environment every time, but you do need to activate it every time in order to use the packages installed in it. This is done using source activate IntroToTensorFlow
If you shutdown a kernel, the environment does not get deactivated automatically. To do so, you have to explicitly say source deactivate, or activate a separate environment using source activate xxx, replacing xxx with whatever environment name you want (that you have created previously).
When restarting your PC, (or starting a new session at the command line), you have to manually activate your desired environment to use it. Otherwise, by default, it will be running in your root environment. So, if you've only installed tensorflow in IntroToTensorFlow environment, you have to use source activate IntroToTensorFlow every time in order to use it.
Take a look here for more info
I have installed an additional Anaconda environment, running python 3.5, so that now I have:
# conda environments:
#
python3.5 /Users/user/anaconda/envs/python3.5
root * /Users/user/anaconda
The Anaconda documentation says that I need to do source activate python3.5 to activate the new environment. What it does not mention is that activate only works in bash. I'm using tcsh, so that I currently have to switch to bash, issue the command above, and switch back to tcsh every time I open a new shell.
How can I ease this process?
If you really want to stay inside this shell, you need to replicate the logic that the activate script does for bash in your shell. I have found a gist that might work:
https://gist.github.com/mikecharles/f09486e884a0b41e1e8f
I've installed Anaconda and created two extra environments: py3k (which holds Python 3.3) and py34 (which holds Python 3.4). Besides those, I have a default environment named 'root' which the Anaconda installer created by default and which holds Python 2.7. This last one is the default, whenever I launch 'ipython' from the terminal it gives me version 2.7. In order to work with Python 3.4, I need to issue the commands (in the shell)
source activate py34
ipython
which change the default environment to Python 3.4. This works fine, but it's annoying since most of the time I work on Python 3.4, instead of Python 2.7 (which I hold for teaching purposes, it's a rather long story). Anyway, I'll like to know how to change the default environment to Python 3.4, bearing in mind that I don't want to reinstall everything from scratch.
If you just want to temporarily change to another environment, use
source activate environment-name
ETA: This may be deprecated. I believe the current correct command is:
source conda activate environment-name
(you can create environment-name with conda create)
To change permanently, there is no method except creating a startup script that runs the above code.
Typically it's best to just create new environments. However, if you really want to change the Python version in the default environment, you can do so as follows:
First, make sure you have the latest version of conda by running
conda update conda
Then run
conda install python=3.5
This will attempt to update all your packages in your root environment to Python 3 versions. If it is not possible (e.g., because some package is not built for Python 3.5), it will give you an error message indicating which package(s) caused the issue.
If you installed packages with pip, you'll have to reinstall them.
Overview
Some people have multiple Conda environments with different versions of Python for compatibility reasons. In this case, you should activate the desired default environment in the shell initialization file (e.g., .bashrc, .zshrc). With this method, you can preserve the versions of Python you use in your environments.
The following assumes environment_name is the name of your environment
Mac / Linux:
Edit your bash profile so that the last line is conda activate environment_name. In Mac OSX this is ~/.bash_profile, in other environments this may be ~/.bashrc
Example:
Here's how I did it on Mac OSX
Open Terminal and type:
nano ~/.bash_profile
Go to end of file and type the following, where "p3.5" is my environment:
conda activate p3.5
Exit File. Start a new terminal window.
Type the following to see what environment is active
conda info -e
The result shows that I'm using my p3.5 environment by default.
For Windows:
Create a command file (.cmd) with activate environment_name and follow these instructions to have it execute whenever you open a command prompt
Create a batch file command, e.g. "my_conda.cmd", put it in the Application Data folder.
Configure it to be started automatically whenever you open cmd. This setting is in Registry:
key: HKCU\SOFTWARE\Microsoft\Command Processor
value: AutoRun
type: REG_EXPAND_SZ
data: "%AppData%\my_conda.cmd"
from this answer: https://superuser.com/a/302553/143794
Under Linux there is an easier way to set the default environment by modifying ~/.bashrc or ~/.bash_profile
At the end you'll find something like
# added by Anaconda 2.1.0 installer
export PATH="~/anaconda/bin:$PATH"
Replace it with
# set python3 as default
export PATH="~/anaconda/envs/python3/bin:$PATH"
and thats all there is to it.
For windows Anaconda comes with Anaconda Prompt which is a shortcut to cmd and can be used run conda commands without adding anaconda in PATH variable.
Find the location of it, copy and rename the copy (say myenv_prompt). Right click myenv_prompt and select properties in the context menu.
The Target form of Properties window should already be filled with text, something like %windir%\system32\cmd.exe "/K" C:\Users\xxx\AppData\Local\Continuum\Miniconda3\Scripts\activate.bat C:\Users\xxx\AppData\Local\Continuum\Miniconda3\
There are three parts of this command 1)start ...\cmd.exe 2)run ...\acitvate.bat with environment 3)...\Miniconda3\
Change 3rd part to path of the environment (say myenv) you want as default i.e. fill the Target form something like %windir%\system32\cmd.exe "/K" C:\Users\xxx\AppData\Local\Continuum\Miniconda3\Scripts\activate.bat C:\Users\xxx\AppData\Local\Continuum\Miniconda3\envs\myenv
Now myenv_prompt will act as shortcut to start cmd with myenv as the default environment for python. This shortcut you can keep in start menu or pinned in taskbar.
One advantage of this method is that you can create a few shortcuts each having different environment as default environment. Also you can set the default folder by filling Start in form of the Properties window
Hope this helps
PS:It is not required to find Anaconda Prompt and can be done by changing target of any shortcut. But you will require to know path of cmd.exe and activate.bat
Just activate your py34 environment when you load your terminal/shell.
If you use Bash, put the line:
conda activate py34
in your .bash_profile (or .bashrc):
$ echo 'conda activate py34' >> ~/.bash_profile
Every time you run a new terminal, conda environment py34 will be loaded.
The correct answer (as of Dec 2018) is... you can't. Upgrading conda install python=3.6 may work, but it might not if you have packages that are necessary, but cannot be uninstalled.
Anaconda uses a default environment named base and you cannot create a new (e.g. python 3.6) environment with the same name. This is intentional. If you want your base Anaconda to be python 3.6, the right way to do this is to install Anaconda for python 3.6. As a package manager, the goal of Anaconda is to make different environments encapsulated, hence why you must source activate into them and why you can't just quietly switch the base package at will as this could lead to many issues on production systems.
Change permanent
conda install python={version}
Change Temporarily
View your environments
run conda info --envs on your terminal window or an Anconda Prompt
If It doesn't show environment that you want to install
run conda create -n py36 python=3.6 anaconda for python 3.6 change version as your prefer
Activating an environment (use Anaconda prompt)
run activate envnme envnme you can find by this commandconda info --envs as a example when you run conda info --envs it show
base * C:\Users\DulangaHeshan\Anaconda3
py36 C:\Users\DulangaHeshan\Anaconda3\envs\py36
then run activate py36
to check run python --version
In Windows, it is good practice to deactivate one environment before activating another.
https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html?highlight=deactivate%20environment
If you want Anaconda Navigator to default to Virtual Env you created, go to file > Preference and select default conda env in drop down lint:
If you want Anaconda command automatically opens to virtual env without having to type activate envName, do this:
Right click on conda shortcut > go to properties and change the Target to something like this:
%windir%\System32\cmd.exe "/K" C:\Anaconda\Scripts\activate.bat C:\Anaconda\envs\p37
Optionally you can set your default working dir as well, like I did in snapshop below:
gl
On Windows, create a batch file with the following line in it:
start cmd /k "C:\Anaconda3\Scripts\activate.bat C:\Anaconda3 & activate env"
The first path contained in quotes is the path to the activate.bat file in the Anaconda installation. The path on your system might be different. The name following the activate command of course should be your desired environment name.
Then run the batch file when you need to open an Anaconda prompt.
Here is the solution I found for autoactivating my preferred environment on a Windows 10 system:
Open anaconda prompt & use 'conda env list' to find the location of the environment you wish to use.
Go to the start menu, right-click 'Anaconda Prompt' and go to file location.
Create a copy of this shortcut file
Open its properties & change the target to the location of your preferred environment.
Now every time you open anaconda prompt through this shortcut it will automatically load your chosen environment.
activate.py is hardcoded to emit conda activate base\n into your shell profile when you evaluate the shell hook produced by conda shell.zsh hook.
you can suppress this hardcoded "auto-activate base" via:
conda config --set auto_activate_base false
then, in ~/.zshrc, ~/.bashrc or wherever you source your shell profile from, you can append the following (after the conda shell hook) to explicitly activate the environment of your choosing:
conda activate py34
I wasn't satisfied with any of the answers presented here, since activating an environment takes a few seconds on my platform (for whatever reason)
I modified my path variable so that the environment I want as default has priority over the actual default.
In my case I used the following commands to accomplish that for the environment "py35":
setx PATH "%userprofile%\Anaconda3\envs\py35\;%PATH%"
setx PATH "%userprofile%\Anaconda3\envs\py35\Scripts;%PATH%"
to find out where your environment is stored, activate it and enter where python.
I'm not sure yet if this approach has any downsides. Since it also changes the default path of the conda executable. If that should be the case, please comment.
For Jupyter and Windows users, you can change the Target path in your Jupyter Notebook (anaconda3) shortcut from C:\Users\<YourUserName>\anaconda3 to C:\Users\<YourUserName>\anaconda3\envs\<YourEnvironmentName>
you could do the same thing for the Anaconda Prompt..etc.
After changing the path you can check your active environment by opening a terminal in Jupyter and run conda info --envs.
I got this when installing a library using anaconda. My version went from Python 3.* to 2.7 and a lot of my stuff stopped working.
The best solution I found was to first see the most recent version available:
conda search python
Then update to the version you want:
conda install python=3.*.*
Source: http://chris35wills.github.io/conda_python_version/
Other helpful commands:
conda info
python --version
Create a shortcut of anaconda prompt onto desktop or taskbar, and then in the properties of that shortcut make sure u modify the last path in "Target:" to the path of ur environment:
C:\Users\BenBouali\Anaconda3\ WILL CHANGE INTO
C:\Users\BenBouali\Anaconda3\envs\tensorflow-gpu
preview
and this way u can use that shortcut to open a certain environment when clicking it, you can add it to ur path too and now you'll be able to run it from windows run box by just typing in the name of the shortcut.
Tried both source activate default_3_9 and source conda activate default_3_9
but worked conda activate default_3_9
I'm trying to update Anaconda in order to use Python 3.10.4 and then Spyder 5.3.2. Actually, I wanted to set the Python interpreter used by Pycharm inside the Spyder console but it required the newest Spyder version. I didn't try all the possible solutions (it's pending for me to use the window batch and modifying path solutions given here) but:
Since I couldn't update the Anaconda base due to the well-known error on the "Solving environment". Then Python and Spyder remain the same.
Creating a new env allows to get the last Python and then his newest Spyder version but it doesn't actualize the Anaconda shortcuts and even the Anaconda navigator if you set it to this new env still has some inconsistencies like keeping the older Spyder version in his menu.
Besides, on point 2, changing the shortcuts target path doesn't work for me.
Finally, I create a new shortcut of the Spyder file from the Scripts folder inside the environment directory ( C:\Users<userName>>\Anaconda3\envs<EnvName>\Scripts )
I couldn't use the default Anaconda shortcuts but I have what I wanted and quick access.
So far I've been able to work with Python 3 and Xcode 5.1.1 by editing the scheme, pointing the Run executable to python3 and then adding some arguments as in the following picture:
But now I would like to work with isolated Python environments through virtualenv. I can do this through the terminal, as I just have to type workon pydev where pydev is my Python environment. But I have no idea of how to modify my Xcode configuration so that I can run within a Python environment. Is there an environmental variable or something I can set to make this happen?
UPDATE
So I have a partial answer but I still can't make it work on Xcode. I created a simple bash script:
#!/usr/bin/env bash
source ~/.bash_profile
workon pydev
python3 src/__main__.py
The source is needed because otherwise the workon command doesn't work (another file provided by the virtualenv package is sourced inside .bash_profile. I can run the script in the Terminal, but the problem now is that Xcode doesn't like it:
Xcode cannot run using the selected destination.
Choose a destination with a supported architecture in order to run on this system.
I solved it, so you have to add the following pre-action to Xcode.
The source is needed because otherwise the workon command doesn't work (another file provided by the virtualenv package is sourced inside .bash_profile.