首页 > 解决方案 > 适用于 Linux 的 Windows 子系统 (WSL) 上的 virtualenv

问题描述

我正在使用 Windows 10 机器并想设置 WSL 开发环境。我在 WSL 中打开了 vscode 并设置了所有内容。安装 pip 等

我正在使用 pipenv 安装依赖项。这样做之后,我的网站将无法启动,因为找不到 python 包:

Exception has occurred: 

ImportError
Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?
  File "/mnt/d/dev/django_appelent_api/manage.py", line 11, in main
    from django.core.management import execute_from_command_line

The above exception was the direct cause of the following exception:

  File "/mnt/d/dev/django_appelent_api/manage.py", line 13, in main
    raise ImportError(
  File "/mnt/d/dev/django_appelent_api/manage.py", line 22, in <module>
    main()

现在,当我查看virtualenv文件夹(查看包是否在那里)时,我看到了我的 windows venv 和我的 ubuntu venv 之间的区别。Windows 在此文件夹中安装了软件包:

C:\Users\<username>\\.virtualenvs\django_appelent_api-8qkCZVAS\Lib\site-packages

和安装在此文件夹中的 ubuntu:

\\wsl$\Ubuntu-20.04\home\<username>\\.local\share\virtualenvs\django_appelent_api-PLA7kcVH\lib

这可以解释为什么我有这个错误吗?Windows 安装在 lib/site-packages 中,而 ubuntu 在它们之间放置了一个名为 python3.8 的文件夹。

我的调试文件(在 VSCODE 中)如下所示:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
    
        {
            "name": "Python: Django",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}\\manage.py",
            "args": [
                "runserver"
            ],
            "django": true
        },
        {
            "name": "Python: Django on WSL",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/manage.py",
            "args": [
                "runserver"
            ],
            "django": true
        }
    ]
}

标签: pythonvirtualenvwindows-subsystem-for-linuxvscode-remote

解决方案


推荐阅读