Trying to understand which environment is active when launching Jupyter from PyCharm pro
According to the docs, when I start a Jupyter notebook from the terminal it should start in the virtual environment (venv). I did that from a project in my PyCharm IDE. My notebook was designed to import some classes I had created in the project but the imports failed. For example, the notebook threw an error that it could not find the sci-kit learn module which is imported by one of the classes I created. However, the module is in the venv. So here is what I see in the terminal (image below). When I first open the terminal it looks like venv is the environment. Playing around it looks like I can change the order of the parenthesized environments but is the order important? What is happening with environments ie. what does Jupyter see, what does the project see?

Please sign in to leave a comment.
Hello,
Jupyter server configuration allows to define an interpreter which should be activated. See the details here https://www.jetbrains.com/help/pycharm/configuring-jupyter-notebook.html#configure-server
Please let me know if you have any questions.
Hi Antonina and thank you for your reply. Yes, I saw that and my configuration is below. As you can see, it is set to 'venv' which contains all the required modules BUT as mentioned, unless I instead change the conda environment (to one that contains the desired modules) then my imports fail. Again my question is why does my environment seem to depend on my 'conda' settings instead of 'venv'.
Also I had sub-questions to understand the information that I see in the terminal:
Hello,
>As you can see, it is set to 'venv' which contains all the required modules BUT as mentioned, unless I instead change the conda environment (to one that contains the desired modules) then my imports fail.
Please attach screenshots preseting:
1. notebook with the resolution error
2. interpreter configuration which includes the packages installed (https://www.jetbrains.com/help/pycharm/installing-uninstalling-and-upgrading-packages.html#interpreter-settings)
My workflow is as follows: I have a project created in PyC pro. In the project I have a notebook directory where I create notebooks. The notebooks import modules they require and also modules that I have created in the project. PyC docs indicate that if I launch a jupyter server from the terminal in the PyCharm pro IDE, then the environment used by that jupyter server will be 'venv'. When I do that I get the following error in my notebook:
BUT 'sklearn' _is_ in my 'venv' (and yes 'sklearn' is the correct name to call for import of scikit-learn)
Have I chosen the correct interpreter? Yes.
Now if I look at the terminal, I have:
Note the parentheses ie. two environments. Now I close the jupyter server and activate a conda environment that has similar packages to my 'venv' so the cli now looks like this:
Note the order and names of the environments and also that now all imports work.
So when I look at that I'm confused: is the jupyter server now using the 'venv' or is it using the conda environment? My expectation was this: when I launch jupyter server from the terminal in the PyCharm IDE it would use the 'venv' regardless of what conda environments are already installed (because yes, I have already a bunch of existing conda environments from previous work that didn't use PyCharm but note that all the packages in 'venv' were installed from the UI in PyCharm Preferences)
FWIW these are my terminal settings. It seems like it should activate the 'venv'
Hello,
Thank you for the screenshots. The fact there are 2 environments activated is definitely not how the things should work. I will investigate it and report as a bug. Please run "deactivate conda" prior to start of the Jupyter server. Will it do the trick?
Here is a corresponding issue https://youtrack.jetbrains.com/issue/PY-49954, please follow it for updates.
Unfortunately, 'deactivate conda' doesn't help. Below is the screenshot of shutting down the running notebook. After shutdown, two environments exist. After 'deactivate conda', the environment returns to my conda 'base' environment. If I try to 'deactivate conda' again to get rid of 'base', I get the deprecation warning. Then when I relaunch Jupyter, I get the import error again because the environment is conda 'base'. But thank you for your time and efforts.
Interesting follow on. Before I shut down PyCharm I decided to look up the manual for conda deactivate in case I had done it wrong. I hadn't but just for fun I ran 'conda deactivate' again. Surprisingly, that gave me now _two_ conda 'base' environments (see double parentheses). Curious I ran 'deactivate' again and it looks like all conda environments have disappeared. However, launching jupyter still does not activate 'venv' and so the imports still fail. HTH
Please do the following:
1. Close PyCharm
2. Open terminal out of PyCharm and run:
2. Open PyCharm's terminal
3. Do you see the expected venv activated? Please run
and provide me with the output.
Run:
Is it successful?
Kind of successful! The conda 'base' no longer appears activated (confirmed by opening new terminal). and 'venv' seems to be the active environment. Importing 'sklearn' seems fine but 'jupyter notebook' does not launch even though both packages are included in 'venv' and the correct server is selected for jupyter.
Please make sure the "jupyter" package is installed. Run in the terminal:
The package for some reason is not in the list.
Actually, jupyter is installed eg. if launch a terminal outside of pycharm it's available.
If I do a 'pip list --local', which I thought provides a list of the 'venv' pip installed packages, 'jupyter' is present but most other packages that are shown for the interpreter under 'preferences' are not.
If I do 'pip list', all packages are present including Jupyter.
If I look under preferences Jupyter is in the installed list for the chosen interpreter. I have uninstalled and reinstalled but that does not seem to make a difference.
I was looking at the interpreter and 'discovered' the option to view the interpreter's paths and below is what I see. There are paths to miniconda, is that appropriate? Is it because my 'base' interpreter is from 'Users/.../miniconda3/bin/python3'?
MikeB,
as long as you use miniconda installation, yes it is ok.
Please run in the teminal in PyCharm:
and send me the output.
I think I got the problem, sorry I should have noticed this before.
The thing is that the virtual environment is configured based on the conda python interpreter , this leads to the dependencies inconsistency.
To be more clear, these steps has been performed https://www.jetbrains.com/help/pycharm/creating-virtual-environment.html , where as the correct way is https://www.jetbrains.com/help/pycharm/conda-support-creating-conda-virtual-environment.html .
Please try:
1. Delete the existing interpreter configuration
2. Delete the venv directory from the Project root
3. Create a new conda env as described https://www.jetbrains.com/help/pycharm/conda-support-creating-conda-virtual-environment.html
Let me know how it goes.
Okay that seems to have done it. I deleted as you recommended and followed the instructions in the link you sent. Things are working as hoped. Some observations:
Anyways, thank you for the prompt assistance, it was much appreciated!
thank you for you feedback!
Please vote for this ticket in order to increase its priority: https://youtrack.jetbrains.com/issue/PY-19974
>my 'venv' directory is at 'Users/.../opt/miniconda3/envs/ . I did not expect that.
The venv location can be specified during the interpreter configuration: