PTVS:如何在一个项目中引用另一个项目中的Python源代码或使用它 [英] PTVS: How to reference or use Python source code in one project from a second project

查看:393
本文介绍了PTVS:如何在一个项目中引用另一个项目中的Python源代码或使用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在带有PTVS的Visual Studio中,我有两个单独的 Python项目,一个项目包含一个名为lib.py的Python源文件,用作功能库,另一个是在以下程序中使用这些功能的主要文件图书馆.我在主体中使用import语句来引用库项目中的函数,但出现以下错误:

没有名为lib的模块

我主要使用Visual Studio在F#中编程,因此我的心态是向其他.NET项目添加引用.

我如何用Python的方式来实现这一目标?

解决方案

Python不像.NET那样使用引用,而是使用要搜索的路径.需要修改搜索路径以包括包含源文件的目录.请参阅:模块搜索路径

在Visual Studio中使用Solution Explorer查看项目时,每个项目都显示Search Paths.

要修改搜索路径:

获取包含要导入源代码的Python文件的目录.

例如lib.py

在解决方案资源管理器中,右键单击lib.py并选择Copy Path

现在要导入模块的项目
例如ConsoleDriver_Python

右键单击Search Paths并选择Add Folder to Search Path...

显示选择文件夹对话框

右键单击并粘贴剪贴板中的路径.还可以通过删除文件名将其更改为目录.

点击Select Folder

现在检查项目以确保Search Path已更新.

现在应该清除导入错误.

In Visual Studio with PTVS I have two separate Python projects, one contains a Python source file named lib.py for use as a library of functions and the other is a main that uses the functions in the library. I am using an import statement in the main to reference the functions in the library project but get the following error:

No module named lib

I primarily program in F# using Visual Studio so my mindset is adding references to other .NET projects.

How do I think in the Pythonic way to accomplish this?

解决方案

Python does not use references like .NET does but uses a path which is searched. The search path needs to be modified to include the directory containing the source file. See: The Module Search Path

Looking at the project in Visual Studio with Solution Explorer shows Search Paths for each project.

To modify the search path:

Get the directory for the Python file containing the source code to import.

e.g. lib.py

In Solution Explorer right click on lib.py and select Copy Path

Now for the project that will import the module
e.g. ConsoleDriver_Python

Right click Search Paths and select Add Folder to Search Path...

which displays a select folder dialog

Right click and paste in the path from the clipboard. Also change it to a directory by removing the file name.

Click Select Folder

Now check the project to make sure Search Path was updated.

The import error should now be cleared.

这篇关于PTVS:如何在一个项目中引用另一个项目中的Python源代码或使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆