How to start jupyter in an environment created by conda? - python

I use conda created an environment called testEnv and activated it, after that I use the command jupyter notebook to call the jupyter editor. It works, but the problem is that, I can only create file in the root environment. How can I create file in testEnv environment?
Here are the steps what I have done:
$ conda create -n testEnv python=3.5 # create environmet
$ source activate testEnv # activate the environmet
(testEnv)$ jupyter notebook # start the jupyter notebook
Here are the result, which shows I can only create file with in "root" but not in "testEnv" (There is only Root, but no testEnv):
In the Tab Conda, I can see the testEnv, but how can I switch to it?

You have two options. You can install the Jupyter Notebook into each environment, and run the Notebook from that environment:
conda create -n testEnv python=3.5 notebook
source activate testEnv
jupyter notebook
or you need to install the IPython kernel from testEnv into the environment from which you want to run Jupyter Notebook. Instructions are here: http://ipython.readthedocs.io/en/stable/install/kernel_install.html#kernels-for-different-environments To summarize:
conda create -n testEnv python=3.5
source activate testEnv
python -m ipykernel install --user --name testEnv --display-name "Python (testEnv)"
source deactivate
jupyter notebook

The answer is that you probably shouldn't do this. Python virtualenvs and Conda environments are intended to determine the resources available to the Python system, which are completely independent of your working directory.
You can use the same environment to work on multiple projects, as long as they have the same dependencies. The minute you start tweaking the environment you begin messing with something that is normally automatically maintained.
So perhaps the real question you should ask yourself is "why do I think it's a good idea to store my notebooks inside the environment used to execute them."

Related

how to use existing conda environment as a AzureML environment

I have created a Azure Compute target and using the notebooks from it. I want to create a conda environment using Notebook Terminal and install my own packages and use it in AzureML experiment environment.
I had the same question and the answer can be found here: https://learn.microsoft.com/en-us/azure/machine-learning/how-to-access-terminal#add-new-kernels
The basic steps are:
Create your conda environment with the terminal
Activate the environment
conda install pip
conda install ipykernel
python -m ipykernel install --user --name newenv --display-name "Python (newenv)"
After reloading the portal you can select the kernel in the list.
Fist make sure we install conda using Notebook Terminal.
We can create an environment from existing conda environment. This make it easy to reuse your local interactive environment in Azure ML remote runs. For example, if we've created conda environment using
conda create -n mycondaenv
We can create Azure ML environment out of that conda environment using
myenv = Environment.from_existing_conda_environment(name="myenv", conda_environment_name="mycondaenv")

Conda install packages from jupyter notebook

I have used !{sys.executable} -m pip install to install packages i want to use in my jupyter notebooks.
Now i also want to install some packaged by conda and use in the same notebooks.
This is the command i run and the error message
They ask us to create a separate conda environment. I am confused about this.
Do we need to create a new conda enviroment everytime we need new packages?
What is the correct way to install and access conda packages on jupyter python notebook?
You shouldn't in principle install from the jupyter notebook but from the terminal/cmd.
You can create an enviroment >conda create --name myenv
And activate it: >conda activate myenv
now everything you install will be restricted to myenv. for example conda install numpy
To use the packages in myenv in jupyter simply open jupyter with your enviroment active:
>conda activate myenv
>jupyter notebook
Note that you have to install jupyter in your enviroment too: conda install jupyter

Jupyter and conda: error when cloning environment

I have created multiple conda environments in order to test compatibility of installed packages. I use conda create -n test02 --clone test01 to create environment test02 as a clone of test01. I activate test02, install new packages and start instance of python from which I import new packages with no problem. The problem arises when I launch jupyter notebook or qtconsole and try to import newly installed package and get import error: no module named 'xxx'. I do a sys.executable and see that jupyter is running python from the old environment (test01) I cloned the new one from. Why is this happening? Can I change it in config file somewhere and where might I find this file?
edit: more info
jupyter --paths for both environments share path entry for config and data in the same environment directory
(test01) PS C:\Users\Aka> jupyter --paths
config:
C:\Users\Aka\.jupyter
c:\users\aka\miniconda3\envs\test01\etc\jupyter
C:\ProgramData\jupyter
data:
C:\Users\Aka\AppData\Roaming\jupyter
c:\users\aka\miniconda3\envs\test01\share\jupyter
C:\ProgramData\jupyter
runtime:
C:\Users\Aka\AppData\Roaming\jupyter\runtime
(test01) PS C:\Users\Aka> conda activate test02
(test02) PS C:\Users\Aka> jupyter --paths
config:
C:\Users\Aka\.jupyter
c:\users\aka\miniconda3\envs\test01\etc\jupyter
C:\ProgramData\jupyter
data:
C:\Users\Aka\AppData\Roaming\jupyter
c:\users\aka\miniconda3\envs\test01\share\jupyter
C:\ProgramData\jupyter
runtime:
C:\Users\Aka\AppData\Roaming\jupyter\runtime
edit2: I forgot to mention that I installed Jupyter using pip.
to recreate the problem I did:
conda create -n env1
conda activate env1
pip install jupyter
jupyter --paths
config:
C:\Users\Aka.jupyter
c:\users\aka\miniconda3\envs\env1\etc\jupyter
C:\ProgramData\jupyter
data:
C:\Users\Aka\AppData\Roaming\jupyter
c:\users\aka\miniconda3\envs\env1\share\jupyter
C:\ProgramData\jupyter
runtime:
C:\Users\Aka\AppData\Roaming\jupyter\runtime
conda create --clone env1 -n env2
conda activate env2
jupyter --paths
config:
C:\Users\Aka.jupyter
c:\users\aka\miniconda3\envs\env1\etc\jupyter
C:\ProgramData\jupyter
data:
C:\Users\Aka\AppData\Roaming\jupyter
c:\users\aka\miniconda3\envs\env1\share\jupyter
C:\ProgramData\jupyter
runtime:
C:\Users\Aka\AppData\Roaming\jupyter\runtime
If I were to install jupyter with conda into a brand new environment and then clone the environment, jupyter behaves as expected. Note: I installed jupyter with pip because I am using python 3.5 because of other packages I need and installing jupyter with conda in my environments kept breaking it. I reached out to conda-forge for help but they said "sorry, we don't support python 3.5".
Jupyter only needs to be install in a single location - either a Conda env or at the system-level.
Jupyter installed in Conda env
To use other envs as kernels, one needs to install nb_conda_kernels in the env with Jupyter, and ipykernel in any env you wish to use as a kernel. Always launch jupyter notebook from the env with Jupyter and the others will be available automatically.
Jupyter outside Conda
If Jupyter is installed at a system level, one must manually register the Conda envs one wishes to use as kernels:
conda activate my_env
conda install ipykernel
python -m ipykernel install --user --name my_env_name
Then launch Jupyter from anywhere.

check which conda env jupyter console is using

I have a conda environment and I would like to run a jupyter console within that environment. I do the usual source activate myenv and then jupyter console. The source activate myenv works since which python points to the right path. However, it doesn't seem like the jupyter console is picking up the right env. I have done this:
python -m ipykernel install --user --name myenv --display-name "Python (myenv)"
I have nb_conda and nb_conda_kernels installed. What's going on with my setup? I have Ipython 6.4.0, Python 3.6.5. Also, coming from an R background, I find it weird that I'm spending so much time on the basic setup where in R things just work. Is there something I'm missing or doing wrong? How can i check which env Ipython is running in?
sys.executable does indeed give you that info. This is how one should actually start the correct kernel (provided by How to start an ipython shell(not notebook) within a conda or virtualenv):
source activate myenv
python -m ipykernel install --user --name myenv --display-name "myenv"
jupyter console --kernel myenv
To get a list of the kernels that can be used:
jupyter kernelspec list
import sys
print(sys.executable)
Your conda environment is just a unique interpreter executable with its own PATH, etc. You could then regex on the string of its location to get the environment name.

Anaconda: Undo conda create

I have anaconda3 using python 3.5. I wanted to experiment with something in python 2.7 so I opened the anaconda command prompt and ran:
conda create -n py27 python=2.7 anaconda
In retrospect that may have been overkill; I didn't need everything in anaconda. I now want to remove it. What I tried
I looked for a uninstall executable for the py27, but couldn't find one
the docs say to: conda - conda uninstall (but that returned: could not locate 'conda--'
I also tried looking in control manager (windows) to see if it was available to uninstall, but it was not
Question: How do I undo conda create -n py27 python=2.7 anaconda? That is to say, I want to go back to my original anaconda3 python 3.5 and no py27.
When you use the create command, you are not replacing your current environment, just creating a new one.
You can see a list of your environments using the following command:
conda info --envs
Then, you can activate a specific environment using (replace py27 by the name of the environment):
On windows: activate py27
On linux: source activate py27
To delete the newly created environment use the following command:
conda remove --name py27 --all
The "all" parameter will also delete any configuration and packages installed with the environment.
Edit
New environments are installed inside the envs folder of your anaconda root so after removing it you can check the folder in case there is anything left, e.g.:
C:\Anaconda3\envs\py27
More info: https://conda.io/docs/user-guide/tasks/manage-environments.html
You only have to delete the environment you do not need any longer:
$ conda env remove -n <env_name>
In your case:
$ conda env remove -n py27
more info here

Categories

Resources