简体   繁体   English

Visual Studio 2017中的Python无法通过搜索路径找到模块

[英]Python in Visual Studio 2017 fails to find modules via the Search Paths

I am trying to add an existing python project with a python virtual environment to visual studio 2017 so I can debug it. 我正在尝试将具有python虚拟环境的现有python项目添加到Visual Studio 2017,以便我可以对其进行调试。

The project also has some external dependencies, 该项目还具有一些外部依赖性,

Everything is arranged in the following folders 一切都安排在以下文件夹中

C:\\myproject\\code

C:\\myproject\\portablepython

C:\\myproject\\pylibrary

I created the project using File->New Project->Python->From Existing Python Code 我使用File->New Project->Python->From Existing Python Code创建了项目

I added PortablePython virtual environment using Tools->Python->Python Environments using C:\\myproject\\portablepython as prefix path, C:\\myproject\\portablepython\\python.exe as Interpreter path C:\\myproject\\portablepython\\wpython.exe as Windowed Interpreter path Language auto detected as 3.6, 64 bit, and I even added C:\\myproject\\pylibrary as the path environment variable. 我使用Tools->Python->Python Environments将C:\\ myproject \\ portablepython作为前缀路径,将C:\\myproject\\portablepython\\python.exe作为解释器路径C:\\myproject\\portablepython\\wpython.exe作为窗口添加了PortablePython虚拟环境解释器路径语言自动检测为C:\\myproject\\pylibrary位,我甚至添加了C:\\myproject\\pylibrary作为路径环境变量。

I also added pylibrary to project search paths so that it looks like ..\\pylibrary 我还在项目搜索路径中添加了pylibrary,使其看起来像..\\pylibrary

My issue is when I build the project, it fails on the very first pylibrary import with ImportError("cannot import name 'mdevice'",) on the line 我的问题是,当我构建项目时,它在第一个pylibrary导入失败,并在该行上出现了ImportError("cannot import name 'mdevice'",)

from mdevice import mdevice

And this module is clearly in the C:\\myproject\\pylibrary path 这个模块显然在C:\\myproject\\pylibrary路径中

What's more interesting is when I remove portablepython from Python environments, and it defaults to Python 3.6, it finds the first module, but it can't find the second module on the line below the first one. 更有意思的是,当我从Python环境中删除portablepython时,它默认为Python 3.6,它找到第一个模块,但是找不到第一个模块下面的第二个模块。

Both modules are in C:\\myproject\\pylibrary 这两个模块都在C:\\myproject\\pylibrary

I don't know what is wrong with Visual Studio here, seems like a bug to me 我不知道这里的Visual Studio有什么问题,对我来说似乎是个错误

Side note: I had to create the virtual environment using Tools->Python->Python Environments because when I right click on project and select Add Virtual Environment and select C:\\myproject\\portablepython it actually shows "We Cannot Create a Virtual Environment at the Specified path. Choose a folder that does not exist, is empty, or contains and existing virtual environment" eventhough it clearly contains the correct python files in this folder. 旁注:我必须使用Tools->Python->Python Environments创建虚拟环境,因为当我右键单击项目并选择Add Virtual Environment并选择C:\\myproject\\portablepython它实际上显示"We Cannot Create a Virtual Environment at the Specified path. Choose a folder that does not exist, is empty, or contains and existing virtual environment"文件夹"We Cannot Create a Virtual Environment at the Specified path. Choose a folder that does not exist, is empty, or contains and existing virtual environment"尽管该文件夹中显然包含正确的python文件。

Python paths aren't set specifically by python code or project files, they are set as system path variables. Python路径不是由python代码或项目文件专门设置的,它们被设置为系统路径变量。 In order to add the modules and the interpreter, I needed to add a PYTHONPATH to system variables and have it point to the folder containing modules, then add the Python.exe path to the top of the path list 为了添加模块和解释器,我需要在系统变量中添加PYTHONPATH并将其指向包含模块的文件夹,然后将Python.exe路径添加到路径列表的顶部

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM