How to switch python env in Jupyter - python

My development environment is Anaconda3 with a virtual environment "OpenCV3".
I can run Jupyter in my main enviroment, but I don't know how to run jupyter in virtual environment "OpenCV3".
could you please show me how to do it?

The following options are available (there may be others available)
change conda environment from the terminal (using the appropriate command corresponding to your OS) and run jupyter notebook.
Install an ipython kernel for each environment as described in this answer. This will enable you to switch between environments directly in Jupyter, without having to manually switch among environments, from the Kernel>Change Kernel menu.

Related

Conda environment not showing up in VS Code

I installed miniconda on Windows 10 and created an environment (I followed this guide: https://www.notion.so/shashankkalanithi/Setting-Up-Conda-Environment-ba83f7f019ea44b9af37588eed419eb9). However when I open the VS Code I don't see that environment python interpeter on kernel list. There is only one interpreter on the list: ~\Miniconda3\python.exe
How can I fix this?
in vscode press ctrl+shift+p and type python:Select Interpreter you should see all the environment there. If it does not appear create a .py file and try again. also you can press the reload icon on the search bar where you typed python:select interpreter.
You can try to follow methods from
vscode - Create a conda environment
Additional notes:
...To ensure the environment is set up well from a shell perspective, one
option is to use an Anaconda prompt with the activated environment to
launch VS Code using the code . command. At that point you just need
to select the interpreter using the Command Palette or by clicking on
the status bar.
Firstly you need to create an environment with python in it otherwise it won't recognize it. Create an environment like this first
conda create --name tf26 python==3.10
Use your preferred name and python version here thereafter restart VS Code. You will definitely see your env.
Note:
You can delete any unused env if want like this
conda env remove --name <env_name>
I wanted to use the new environment as a Jupyter kernel and had to install the jupyter package for it to show up in the kernel selection of VSCode. You can install it by running conda install jupyter.
I finally resolved the problem.
This thread says that you need to create the conda environment passing the python argument:
conda create -n your_env_name python=3.7
Doing this the environment appears in the Select interpreter to start Jupyter server options.
The extension automatically looks for interpreters in the following locations:
Conda environments that contain a Python interpreter. VS Code does not
show conda environments that don't contain an interpreter.
After you create a conda environment, you need to activate it and install some packages in order to get the python interpreter. And remember to reload the VSCode. If it still does not exist, you can try to choose Enter interpreter path, to point the path manually.
In your project .vscode/settings.json file, just replace the old python.pythonPath setting with the new one (or add if non-existing) python.defaultInterpreterPath and it will work. The value for the setting is the path to the venv you're using in your project.
Afterward, Ctrl+Shift+P via Python: Select Interpreter will allow you to choose a different interpreter.
I had the same problem.
After I opened Anaconda Prompt as Administrator and created the environment, I saw it in VS code
Open “Anaconda Prompt” from the Windows start button as
“Administrator.”
The reason why vscode doesn't show the environment is that it doesn't have a python interpreter in it because of inheriting or something.
The solution is also simple. Just manually install python in that environment.
conda install python

Explanation about Miniconda environments

I'm new using Jupyter on Miniconda and I was having a problem while importing packages (ImportError: DLL load failed ), looking for answers the solution was to initialize a base environment in my bash.
I used to initialize jupyter typing jupyter notebook in bash, but using the solution given, I have to activate conda activate bash and then type jupyter notebook. What is the difference between starting Jupyter the way I used to and this new way?
conda activate command activates a virtual environment. It is an isolated environment so all packages you installed in the virtual environment cannot be used outside it. When you start bash, you are in the base environment and it seems that you installed your Jupiter in bash environment so you cannot use bash's Jupiter in base environment and vice versa. It may be a little annoying at the beginning, but it can let you use different environments for different purposes. For example, since pip only allows one version of a specific package to be installed, different environments can let you test a new version of a package without breaking the functionality of the original program.

Jupyter Notebook opens a blank page for all environments except base(root)

I am trying to follow the TensorFlow API documentation. I have done all the steps and activated my tensorflow_gpu environment. In the C:\TensorFlow\models\research\object_detection I typed "jupyter notebook" and it opens me a blank page http://localhost:8889/tree.
I tried to open the Jupyter Notebook in my base(root) and it opens the dashboard but for the other environments it only gives me a blank page.
How can I avoid that? Thank you.
I tried to delete and re-install jupyter notebook for that specific environments. I tried to upgrade and downgrade the version of it. Nothing worked yet.
You can still run the Jupyter Notebook in your base environment and make the notebook run with a different kernel (like if you are running the notebook in a different environment). In order to do so you need to install these packages in all of your environment:
conda install nb_conda nb_conda_kernels ipykernel
By doing so you will be able to see the kernels of all of your environments directly in base.
You will be able to start jupyter notebook in base, and once the notebook is running in your browser you can decide which kernel to use.
In the above picture I started Jupyter Notebook in my base environment, and having installed those packages in all the environments let me run any notebook with any kernel.
NB: My other environments are tf_lite1.13, tf_lite2, tf_night. Root will run the base kernel.

When using Jupyter, I have to install python modules with Conda and not cmd prompt. Why?

I'm pretty new to programming so forgive my ignorance.
When installing certain python packages/modules in the cmd prompt I am able to import them fine when using Jupyter Notebook. But some modules Jupyter Notebook cannot import without installing via Conda first. Why is this?
The problem seems to be related to system and environment that you are using and not the programming :)
Since you are a beginner, let us understand the concepts first rather than solving the problem.
Python code is run on an interpreter that is installed on your machine.
Jupyter is a web application that takes code using a given language's interpreter. So Jupyter, on its own doesn't run your code. It uses the interpreter installed on a system(your local machine).
Conda is a package manager and also an environment manager. That means using conda, you can create a virtual environment on your machine and that virtual environment can have its own installation of an interpreter. This virtual environment can have its own copy of the packages/modules as well.
Now comes the best part: The jupyter notebook can be asked to use any of the interpreters including the ones installed on a virtual environment.
So most likely, you are running the jupyter notebook from an environment which doesn't have the required dependencies. So either run the jupyter notebook outside the environment or install the required packages in the environment where your jupyter notebook is running.
To know which environment is being used by your jupyter notebook, run below lines from the jupyter notebook cell:
import sys
sys.executable
If you don't get something like /usr/bin/python then the jupyter is running inside an environment. So you have to install all the packages/modules inside that environment only.

How to use CPLEX with PYTHON on windows

Though I've already install Cplex, and could import cplex in Jupyter Notebook.
When I use "cplex.infinity", error arises and it says "module 'cplex' has no attribute 'infinity'"
I don't know the reason, hoping you could help me.
This is because virtual environments in Jupyter are different.If you don't tell your Jupiter kernel which environment it should use it won't use your environment from python terminal (even launching the jupyter notebook command with this virtual environment activated).
You can follow this guide.
Basically:
You have to install your virtual environment for Jupyter
You have to tell the kernel to use this environment in the top bar of your notebook
You will have then all the dependencies installed in your virtual environment available from Jupyter.
Cheers!

Categories

Resources