Search code examples
jupyter-notebookinterpreterpython-venv

How to create a venv based on an existing interpreter?


In the system I work on (Ubuntu), the admin has installed all necessary packages (torch,tensorflow,cuda, matplotlib, etc) in this path:

/usr/bin/python3.10

enter image description here enter image description here

If I check "inherit global site-packages" I can access all necessary packages. I want to know how to create a virtual env based on this interpreter to run Jupiter Notebook?

I don't want to use Pycharm's Jupyter capability. I want to use Jupyter notebook independently and use this venv.

To create a new virtual env I've done these steps:

virtualenv -p /usr/bin/python3.10 MY-env
source ./MY-env/bin/activate

And then to add my venv to jupyter kernels:

pip install ipykernel 
python -m ipykernel install --user --name=MY-env

In the Terminal when I run jupyter-lab, I select My-env from the "Change Kernel" option. for testing I imported torch, but I got the error:

ModuleNotFoundError: No module named 'torch'

While torch is installed and exists in the site-packages. It means it hasn't detected site-packages of /usr/bin/python3.10.


Solution

  • My problem is solved.

    This solution worked for me:

    1. Go to the pyvenv.cfg file in the Virtual environment folder
    2. Set the include-system-site-packages to true and save the change
    3. Reactivate the virtual environment.