PTVS: how to link or use Python source code in one project from a second project

In Visual Studio with PTVS, I have two separate Python projects, one containing the Python source file named lib.py for use as a function library, and the other the main one that uses the functions in the library. I use the import statement mainly to refer to functions in the library project, but I get the following error:

No module named lib

I primarily program in F # using Visual Studio, so my thinking adds links to other .NET projects.

How can I think in a pythonic way for this?

+7
python module ptvs
source share
3 answers

Python does not use links such as .NET, but it does use path searching. The search path must be changed to include the directory containing the source file. See: Module Search Path

Looking at a project in Visual Studio using Solution Explorer shows the search paths for each project.

enter image description here

To change the search path:

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

eg. lib.py

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

enter image description here

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

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

enter image description here

which displays a dialog with a folder

enter image description here

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

enter image description here

Click "Select Folder"

Now check the design to make sure the Search Path has been updated.

enter image description here

The import error should now be cleared.

+15
source share

I just wanted to add below in addition to the verified answer for a very specific scenario.

Recently, I was asked to fix the same problem that OP was experiencing for a work computer in which user accounts were recently included in the new domain.

Setup: Visual Studio 2013 PTVS 2.2.30718 Anaconda 3.5

Basically, Anaconda was installed for localmachine / UserA.

Once users were transferred to the new domain (newdomain / UserA), the Python environment needed to be updated from VS2013 by clicking View> Other Windows> Python Environment.

Once this has been installed, python scripts will work as expected, although none of the links in the search folder will work. Then they were deleted and re-added, but to no avail.

Various other things were tested, including creating completely new projects and linking them using search paths, but to no avail.

The only thing that fixed the problem was to reinstall the Python environment (in my case Anaconda3) outside of the user account (by clicking the "for all users using administrator rights" button during installation).

Then I restarted, deleted and added the search folders again, and python worked as expected, including all the search paths.

I hope this helps someone as I just spent a lot of time solving it ...

D :)

+3
source share

Or you can do it in code with the following:

 sys.path.append("search path") 

So that the code can be run outside of the IDE.

0
source share

All Articles