How to test in VSCode a postgreSQL SQL expression in Python's interactive notebook (= Jupyter Notebook, iPython) in a venv with package manager conda?
Test in VSCode a postgreSQL SQL expression in Python’s interactive notebook (= Jupyter Notebook, iPython) in a venv with package manager conda
anacondaipythonpostgresqlpythonsql
Best Answer
Sharing this since it took me too long to set this up from the scattered guides and hints.
conda create -n venv310 python=3.10
and activate it with:conda activate venv310
.If I open VSCode from a terminal that has activated the venv310, VSCode falls back to the base env as soon as I run code with Shift+Enter:
Press Ctrl+Shift+P → "Python: Select interpreter" → choose venv310 (this is what I could already do before, but now, when running some code, it did not fall back to the base env). See Activating Anaconda Environment in VsCode - Stack Overflow.
Open the interactive Window with Ctrl+Shift+P → "Jupyter: Create Interactive Window"
I started the interactive window just with a right-click on the
%load_ext sql
line that I had in the normal Python code editor. Or you press Shift+Enter in that line.See How do I find/excute Python Interactive Mode in Visual Studio Code? - Stack Overflow.
Then it shows:
Click Install.
Thus, if you see this as well, run
conda install -n venv310 ipykernel --update-deps --force-reinstall
in the venv310.%load_ext sql
in the iPython terminal, either by typing it in the terminal, or by running the magic from the Python editor (check right-click to see how to run in interactive window).If you see:
or if you run the following magic and see one of these:
then you need to run in the venv310:
(and not the pip installer
pipenv install ipython-sql
like it is written in the ipython-sql guide)See UsageError: Line magic function
%sql
not found - Stack OverflowBut that does not harm.
(See: What is the format for the PostgreSQL connection string / URL? - Stack Overflow)
with the output afterwards. For a displaylimit, see the ipython-sql guide --> "Configuration".