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

3

8

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?

Alethaalethea answered 18/2, 2016 at 14:57 Comment(0)
A
16

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.

Alethaalethea answered 18/2, 2016 at 16:10 Comment(5)
Of interest: PTVS - Projects - Search Path Official documentation on PTVS search paths.Alethaalethea
The link in the comment above is now dead and redirects to Python Projects. After glancing over the new page there may be a different way to accomplish this as PTVS is evolving. When I get time I plan to revise the answer but right now am neck deep in Prolog.Alethaalethea
Is this the same as doing a sys.path.append() in code or adding to the PYTHONPATH if running from the command line?Dowdy
@Doodah You should ask that as a new question.Alethaalethea
I just answered my own question in development. The search path in VS is the same as doing a sys.path.append() in code, which is the same as using PYTHONPATH. IMO, one should never rely on side-effects in an IDE to make something work. Therefor, Search Path should be avoided.Dowdy
S
3

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 :)

Shaikh answered 2/2, 2017 at 15:15 Comment(2)
Nice to know. Thanks.Alethaalethea
Good find. Final pointer on this issue - after reinstalling Anaconda with admin privileges, you still need to reboot the machine for Windows to sort it all out and for VS to correctly adjust the search path.Raine
D
0

Or you can do this in code with the following:

sys.path.append("search path")

So that the code can be run outside the IDE.

Dowdy answered 11/1, 2018 at 17:18 Comment(1)
Normally I automatically upvote any reasonable answer to a question. While I see your answer as a valid option, I am hesitant to up vote it being correct as sys.path.insert() might be better, but even more so, I see the information you give as already being present at SO and that this should be a comment with a link. I do understand that it is a good option for others to know about so I won't down vote it.Alethaalethea

© 2022 - 2025 — McMap. All rights reserved.