0

I'm trying to switch to VS Code for my Python learning / development. Still finding my way around.

I suspect this is a "duh" problem, but I'm having trouble with environments. I can select an env in the Terminal with .venv/Scripts/Activate.ps1 (is that the right way to do it?), so I can pip-install modules in the environment. But the code I run in my notebook (using the "Jupyter" & related extensions) doesn't use that env. I can't import the installed modules, because my notebook is using some other env.

I tried Python: Select Interpreter and it's using .venv/Scripts/python.exe but that doesn't seem to change the Jupyter env.

VS has a command to create an env, but I see no way to select the env that the code runs in. Or tell what environment the notebook is running in. VS doesn't seem to pay attention to the .venv dir in my workspace directory, even if I start VS by running code in that dir.

I've googled all over and I can't find an answer. Closest I've found is How can I set up a virtual environment for Python in Visual Studio Code? but I don't think that works for the Jupyter environment.

garyfritz
  • 13
  • 5
  • In regards to ," But the code I run in my notebook (using the "Jupyter" & related extensions) doesn't use that env. I can't import the installed modules, because my notebook is using some other env", you need to install a kernel into that new env and then direct Jupyter that that is a possible kernel to use and then use that. For example, see [here](https://www.geeksforgeeks.org/using-jupyter-notebook-in-virtual-environment/) since it seems you are using venv. – Wayne Nov 09 '22 at 17:43
  • Hm. I had to install ipykernel but then it worked. Kinda. My situation now is that I have two Python files open. Imports (e.g. `import pandas`) work in one file -- and NOT in the other. I'm not sure if selecting the Python interpreter is a global setting or local to one buffer, but I selected my venv in both. How can it work in one buffer and not another? – garyfritz Nov 09 '22 at 22:52
  • Not following your comment there. You should be able to run scripts from inside the environment where Jupyter or IPython is running. If `import pandas` work in either, then you can use `%run .py` inside Jupyter or IPython and it should import pandas. If the import doesn't work in Jupyter or Pandas, run `%conda install -c anaconda pandas` in either if you are using conda as your package manager. If not using conda and installing Python packages with `pip` then use `%pip install pandas` in your notebook or IPython first. Then restart the kernel in Jupyter .... – Wayne Nov 10 '22 at 15:32
  • or quit the IPython console, quit the termimal it is running in, start a new terminal, and restart IPython before using `%run` to run the script or scripts. Often that is easier than sorting out the particular environment. Python should also be using the same environment you are in if you start it up correctly. Like I said it's hard to follow what all is going on from your description. And really it is another question to research and then post about if you don't resolve. – Wayne Nov 10 '22 at 15:35
  • "run scripts from inside the environment where Jupyter or IPython is running" ... not sure what you mean. Do you mean the file / buffer running the Jupyter notebook? I'm a noob and don't get the relationship. But I think I've stumbled across the answer. In VSCode I had selected the Python interpreter, but that didn't help. But I just found the `Notebook` commands, in particular `Notebook: Select Notebook kernel`. That lets me select the Python / kernel for my project -- and now it sees the modules I had installed in that env. Now if I can figure out how to import my module ... – garyfritz Nov 10 '22 at 17:21
  • You wrote in your comment that I didn't understand, " I have two Python files open". I assumed then that you meant Python scripts. They usually have the extension ending in `.py`. If you are using a notebook, that isn't a Python file. That's a Jupyter notebook file with Python code in it. The notebook file extension is `.ipynb`, but from that alone you cannot tell the language the code inside is. And since I thought by " I have two Python files open", you meant Python scripts, I was pointing out you can run scripts inside Jupyter notebooks or IPython. A minor aside since not your real issue. – Wayne Nov 10 '22 at 17:29
  • My bad. Yes, I have/had two Jupyter notebooks. One defines a module, the other uses it. Now I'm struggling with the difficulty of importing the "defining" notebook. I tried `%run` and `import_ipynb`, no luck. I finally decided notebooks are not meant to be imported, so I downloaded it into a .py file. I *think* it's working, though I'm struggling with global vars. This should not be this hard ... – garyfritz Nov 10 '22 at 18:29
  • No, as explained [in the documentation here](https://jupyter-notebook.readthedocs.io/en/4.x/examples/Notebook/rstversions/Importing%20Notebooks.html) notebooks aren't importable as `import` expects/works with Python files. However, you can use [importnb](https://github.com/deathbeds/importnb) to make it easy to import a notebook directly. `%run` pointed at the 'defining' notebook should run that notebook as if it was in the namespace of the notebook where you are issuing the `%run`. You'd want to use [nbdev](https://nbdev.fast.ai/) if you are trying to make modules from notebooks. – Wayne Nov 10 '22 at 18:44
  • I already tried `%run` and `import_ipynb` without success. I'll look at `importnb`. Thanks for your help! – garyfritz Nov 10 '22 at 20:11

0 Answers0