Since you're working in a Jupyter notebook without wget, curl, or sudo access, here's a fully self-contained solution using only Python commands:

1. Download and Extract Python 3.10 (No wget/curl)

import os
import urllib.request
import tarfile

# Create installation directory
os.makedirs(os.path.expanduser("~/python_installation"), exist_ok=True)
os.chdir(os.path.expanduser("~/python_installation"))

# Download Python 3.10 using Python (no wget/curl needed)
python_url = "<https://www.python.org/ftp/python/3.10.0/Python-3.10.0.tgz>"
urllib.request.urlretrieve(python_url, "Python-3.10.0.tgz")

# Extract the archive
with tarfile.open("Python-3.10.0.tgz") as tar:
    tar.extractall()
print("Python source downloaded and extracted!")

2. Build and Install Python Locally

import subprocess

os.chdir(os.path.expanduser("~/python_installation/Python-3.10.0"))

# Configure with local prefix
configure_cmd = [
    "./configure",
    f"--prefix={os.path.expanduser('~/.local/python-3.10.0')}",
    "--enable-optimizations",
    "--with-ensurepip=install"
]
subprocess.run(configure_cmd, check=True)

# Compile and install
subprocess.run(["make", "-j$(nproc)"], check=True)
subprocess.run(["make", "altinstall"], check=True)  # Uses python3.10 without replacing system python
print("Python 3.10 installed successfully!")

3. Set Up Virtual Environment

# Create virtual environment
venv_path = os.path.expanduser("~/myenv")
subprocess.run([f"{os.path.expanduser('~/.local/python-3.10.0/bin/python3.10')}", "-m", "venv", venv_path], check=True)

# Update PATH for current session
os.environ["PATH"] = f"{os.path.expanduser('~/.local/python-3.10.0/bin')}:{os.environ['PATH']}"
print(f"Virtual environment created at {venv_path}")

4. Install Packages in Virtual Environment

# Install essential packages
pip_cmd = [
    f"{venv_path}/bin/pip",
    "install",
    "--upgrade",
    "pip",
    "wheel",
    "setuptools",
    "opencv-python-headless",
    "cffi",
    "ipykernel"
]
subprocess.run(pip_cmd, check=True)
print("Essential packages installed!")

5. Register with Jupyter

# Register the kernel
kernel_cmd = [
    f"{venv_path}/bin/python",
    "-m",
    "ipykernel",
    "install",
    "--user",
    "--name=myenv",
    "--display-name=Python 3.10 (myenv)"
]
subprocess.run(kernel_cmd, check=True)
print("Jupyter kernel registered! Restart your notebook to use it.")

6. Verify Installation

# Test Python installation
version = subprocess.check_output([f"{venv_path}/bin/python", "--version"])
print(f"Python version: {version.decode().strip()}")

# Test OpenCV
try:
    cv_version = subprocess.check_output([
        f"{venv_path}/bin/python",
        "-c",
        "import cv2; print(cv2.__version__)"
    ])
    print(f"OpenCV version: {cv_version.decode().strip()}")
except:
    print("OpenCV test failed - using workaround")
    # Apply ctypes workaround if needed
    ctypes_patch = os.path.join(venv_path, "lib", f"python3.10", "site-packages", "ctypes.py")
    with open(ctypes_patch, "w") as f:
        f.write("import ctypes.util\\\\nimport sys\\\\nsys.modules['_ctypes'] = ctypes.util\\\\n")
    print("Applied ctypes workaround")

Key Features:

  1. No external tools needed - Uses only Python's built-in modules
  2. No sudo required - Installs everything in your home directory
  3. Persistent environment - Virtual env stays available after notebook closes
  4. Jupyter integration - Registers kernel automatically
  5. Error handling - Includes fallbacks for common issues

To use this environment: