Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PTVS: How to reference or use Python source code in one project from a second project

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?

like image 758
Guy Coder Avatar asked Feb 18 '16 14:02

Guy Coder


People also ask

How do I run a project from another project in Python?

You take the code you want to use in both projects, and you put it into a module, which you extract into a third separate project. That project you make into a package, which you can work on separately. You then release version of it, and reuse them in your other projects.

How do I import another Python file into a project?

If you have your own python files you want to import, you can use the import statement as follows: >>> import my_file # assuming you have the file, my_file.py in the current directory. # For files in other directories, provide path to that file, absolute or relative.

How do I add a Python file to Visual Studio project?

Launch Visual Studio and select File > New > Project. In the New Project dialog, search for "Python", select the From Existing Python code template, give the project a name and location, and select OK.

Can you use Python in vs studio?

Visual Studio is a powerful Python IDE on Windows. Visual Studio provides open-source support for the Python language through the Python Development and Data Science workloads (Visual Studio 2017 and later) and the free Python Tools for Visual Studio extension (Visual Studio 2015 and earlier).


2 Answers

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.

enter image description here

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

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 select folder dialog

enter image description here

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

enter image description here

Click Select Folder

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

enter image description here

The import error should now be cleared.

like image 172
Guy Coder Avatar answered Oct 07 '22 01:10

Guy Coder


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

I was recently asked to fix the same problem that the OP was experiencing for a work machine, which had recently had the user accounts migrated over to a new domain.

Setup: Visual Studio 2013 PTVS 2.2.30718 Anaconda 3.5

Basically, Anaconda was installed for localmachine/UserA.

Once the users were migrated over to the new domain (newdomain/UserA), the Python environment had to be updated from within VS2013, by clicking View > Other Windows > Python Environments.

Once that was setup, the python scripts would run as expected, although none of the Search Folder references would work. They were then removed and re-added but to no avail.

Various other things were tried, including setting up totally fresh projects, and linking them using the 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 a user account (by clicking the "for all users, using administrator privileges" option during the install).

Then I restarted, removed and re-added the search folders, and the python worked as expected, including all the search paths.

I hope that helps someone, as I just wasted hours solving it...

D :)

like image 24
Del Avatar answered Oct 07 '22 00:10

Del