I have a project mylib
(a Python library) and a project myapp
(an application that uses mylib
). The two code bases live in unrelated directories. I'm trying to find a setup by which I can edit code in both simultaneously with support from Visual Studio Code.
My plan had been:
- create a single Visual Studio Code workspace
- add the root directories of both projects to that workspace
- create a venv somewhere else
venv/bin/pip install -e
both projects into that venv- select the python interpreter from the venv on the workspace level in Visual Studio Code.
However, Visual Studio Code shows the wiggly lines for symbols from the library that are used in the application, saying the imports cannot be found. When I run venv/bin/python
, it can import the library symbols just fine.
Two questions:
- Why can't Visual Studio Code import the packages that the venv's python can?
- More importantly: what's a good setup for editing two projects simultaneously using Visual Studio Code, I am not stuck on what I tried ...
I have a project mylib
(a Python library) and a project myapp
(an application that uses mylib
). The two code bases live in unrelated directories. I'm trying to find a setup by which I can edit code in both simultaneously with support from Visual Studio Code.
My plan had been:
- create a single Visual Studio Code workspace
- add the root directories of both projects to that workspace
- create a venv somewhere else
venv/bin/pip install -e
both projects into that venv- select the python interpreter from the venv on the workspace level in Visual Studio Code.
However, Visual Studio Code shows the wiggly lines for symbols from the library that are used in the application, saying the imports cannot be found. When I run venv/bin/python
, it can import the library symbols just fine.
Two questions:
- Why can't Visual Studio Code import the packages that the venv's python can?
- More importantly: what's a good setup for editing two projects simultaneously using Visual Studio Code, I am not stuck on what I tried ...
- The steps work for me. Can you provide a Minimal, Reproducible Example? Two projects code samples. – Minxin Yu - MSFT Commented Mar 27 at 6:56
- I think VSC does not "take" the venv that I'm trying to set for the projects. If I create a .venv inside mylib, then running the terminal with that project as the current directory, then "which python" shows .venv/bin/python. If I choose my app project, it shows system python. Even after setting it in that project, or "Select at workspace level". – Johannes Ernst Commented Mar 28 at 23:57
- Did you active the venv? – Minxin Yu - MSFT Commented Mar 31 at 1:22
2 Answers
Reset to default 0Do you really need a separate library in it's own project? Do you plan to build it and ship publicly for all to pypi and git? Or do you just need modules and packages you could reuse across your project?
[1]
In the first case, if your goal is really to develop and build a package that could be installed by others globally, then, first use the uv package manager.
When developing a framework or any library/pypi package, you, of course, want to have a separate project to test it on.
Let say your library project is in ~dev/mylibrary
And your project using it is in ~dev/myapp
From ~dev/myapp
you can simply:
uv pip install -e ../mylibrary
Then you will be able to import mylibrary into myapp without the need to always build it and push to git or pypi, and pulling it every time you update the code.
You also will be able to work on your code in the VS Code or other IDE in 2 projects simultaneously, and quickly changing the code in the library, will immediately work in the app project, so you do not need to run uv again.
However, sometimes, you might face the cache issues. In that case, delete .venv folder, delete all the pycache folders and run uv sync and uv pip install -e ../folder again.
Highly recommend the Project Manager extension for the VS Code. Here are extensions, I am using while developing my own framework and testing it locally.
[2]
In the second case, if all you want is just to share some modules and local packages (folders) across your project and multiple scripts or notebooks, then you can use the Arkalos project structure and read this guide about the code to reuse vs code to run:
https://arkalos/docs/structure/
Create the uv/arkalos project and then all your imports will just work. Your modules will be inside the app folder, while you could import them into notebooks inside the notebooks/ folder or scripts - inside the scripts/ folder.
for the
1. make sure that your scripts have the same py version as your venv, or make sure that there's compatibility between the interpreters used for the scripts and the interpreter in the venv.
2. github codesapces?? although ive never extensively tried them myself, but it might work.