How to save a default python env for VSCode Jupyter Notebooks - python

I've started playing around with the new VSCode notebooks feature and I find one aspect of it particularly annoying.
I open my "jupyter_notebooks" directory the same way I would when using a typical jupyter server, except I open it in VSCode. And I click on a script to bring it into the editor.
However, when I go to run my first cell, it asks me which python environment I want to run it in. This happens every time I start running any script... this is particularly annoying because the "suggested" environment isn't the one that I always use.
Is there a way to set the default python env to a vscode workspace or something so that when I open that folder in vscode it just assumes which env I want to use and stops asking me?
This is an image of the menu that keeps popping up every time I want to run a new script.

You can use "Ctrl+Shift+P" and choose "Python:Select Interpreter"
Then reload window can work.

Related

Pycharm: how to automatically restart Jupyter on project code change?

In my Pycharm projects I use Jupyter to experiment and when something is mature enough I move it to Python files within the project, and make the notebook use it.
From time to time, while running a notebook I change that Python code but am forced to manually restart the server in order to make the notebook use the changed code.
I'd like Pycharm to automatically do it for me.
Is there a way?

Module appearing as uninstalled even though it installed

The title says it all basically. I was trying to code a macro to automate responding to emails, set up the main.py. Just wrote the import and a print. Ran and boom.
Here is a screenshot because I cant actually explain it correctly https://imgur.com/a/faAt7fY
Instead of deleting one version of python and keeping the other(as mentioned in the other answer) you can keep both.
For example if I have python 3.9 and python 3.10 I can use environment variables to tell my system which interpreter I want to use.
For windows
There are two ways to do this on windows computers(one way might work on your computer, the other may not).
First Way
To set two interpreters on windows you can search up "control panel" in your windows search bar. Then go to "system and security". On the top right corner their will be a search bar in it type "advanced system settings" till you get something like this:
Click on "advanced system settings". You will get something like this:
Then click on the environment variables button. It will lead you to a popup that will look like this:
Under user variables, click on the "Path" environment variables. Then press the "Edit" button. This will take you to a place where you can add multiple interpreters. You should know the location of your python.exe file to proceed. If you don't know the location I recommend going to the main python website and going to the setup wizard to see the directory it is installed in then you can trash(delete) that setup wizard.
Now that you know the location to all your interpreters you can one be one click on "New" and add the directories of all your python interpreters(Note: Don't add the python.exe at the end ONLY the directories). Once that is done you can press on "Move Up or Move Down". What this will do is the one that is at the top will be your python interpreter and you can always move it down if you need to use another interpreter(Note: You will only see the results after killing your command prompt and restarting it again. Type python in your command prompt and you can see your python version that you specified in your environment variables).
Second Way
Watch the snippet of this video to learn the second way you can do it on windows.
Mac/Linux
I am not a geek in Linux(In fact I haven't even owned a Mac or Linux in my life). So, my advice would be to just read what I did In Widows way one and just change the environment variables that you would do for Mac/Linux and there would probably be a way to specify which one is the main or most important directory. Watch this video to do what I did in windows.(Feel free to edit this section to add the Mac/Linux way).
Mac Helpful resources:
https://www.youtube.com/watch?v=PUIE7CPANfo
Back to the Main question!
Now you can just install the package that you want on the python version that you want!
Answer: I tried updating to a new version of python but it seems like it didn't
install correctly, so I opened the installer, uninstalled the new python version from there and now everything works correctly. Thanks to everyone that collaborated!

Jupyter notebooks in VSCode not showing up to date python kernel list

For a couple of months I've had issues with working with virtual envs, python notebooks & jupyter in VSCode, for some reason, a lot of the time, it cannot find kernels I added, even though I can see them when running jupyter kernelspec list. It seems random whether they appear or not. Reloading the screen and restarting vscode does not help, neither does disabling and re-enabling the python and jupyter extensions. I tried both from the button, select kernel and through the command palette. I usually start vscode from the command line, with my virtual environment activated. Moreover, a lot of kernels which I've deleted a long time ago still show up in the list.
Vscode and laptop are both up to date.
I've had issues with the integrated jupyter notebooks before, at first formatting and autocomplete didn't work in the notebook (but they did in regular python files).
I'm on MacOS, if that matters.
Sorry to tell that this is a bug, see bug report:
Can't see or select Jupyter kernel's from Kernel drop-down
And now there's a workaround that:
Find where python is installed
Select the command Python: Select Interpreter
Next select Enter interpreter path
Next enter the path into the input box and hit Enter
This interpretr will now work in VS Code and you might need to re-load
VS Code just once more to get it working in jupyter extension.
Please have a try.

Unable to change Python 2.7 to Python 3.8 on Sublime and VS code

I have been trying to change my python interpreter on sublime text and VS code and no matter what I do, both of them refuse to change. It always picks the default Python 2.7 version.
On sublime, I tried to create a new build tool, added the path to Python3 but nothing is working.
And on, VS code, even if I select Python3 from the drop-down menu, it doesn't change anything.
I changed the system's default python interpreter from 2.7 to 3.9 and set the path on bash profile. It fixed the issue for me.
I don't know about Sublime, but for VS Code, there looks to be quite a few things to verify:
Check your User Settings on whether you've set up a default interpreter for your applications
Are you using Window 10 WSL? Then you might want to check out issue 3227 for vscode-remote-release where the comment was:
So I was having this same exact issue until I read this article on a wsl tutorial. In there it says we should have "Remote - WSL" extension installed (already did). In the bottom left corner "Remote - WSL" installs a status button that has the "greater than" and "less than" symbols displayed. If you click that button and tell VSCode to open up in the remote WSL distro environment it fixed my issue where vscode was unable to find an interpreter for python.
For Sublime, you need to hit ⌘B with your script pane focused in order to use your new python3.sublime-build build system.
It looks like you are running your script within Terminus, which is fine, but you need to use the python3 command when running it from the command line, even if that command line is within Sublime.
Please check out my answer here on how to make a Python build system that accepts user input, as the Sublime "console" doesn't allow you to interact with your program. Since you already have Terminus installed, I'd recommend going down to the bottom of the answer and using the Terminus build system. That way, you won't have to deal with Terminal windows opening every time you run a build.
The terminal window uses your PATH, not the IDE settings, so manually typing out python --version isn't testing anything except the PATH variable
In order for the terminal to use the "correct" python binary you've specified, it would need to give the absolute path to it, which I believe using Command+B (using the build options) in Sublime would do. Similarly, you can use the Run/Debug Configurations in VSCode.
Or you can modify your ~/.zshrc to fix your PATH, for example installing pyenv to change the entire system Python version, or the one for the current folder

Mac Swampy (Python learning module) install

I thought I would teach my kids programming and am using "Think Python" by Downey. The book is great except when he gets to installing "Swampy" a Turtle-like learning module. I've spent a few hours trying to figure it out--now I need help.
Can anyone offer a clear, step-by-step set of instructions on how to install Swampy? Please assume zero prior knowledge of Unix, etc.
I'm on OS X (10.6.6). Python is running fine. Tkinter is fine.
Here are the overall installation instructions offered at the author's site:
http://www.greenteapress.com/thinkpython/swampy/install.html
I am stuck at the "Add Swampy to your search path" section.
Here are the additional instructions the author points to regarding the search path:
http://docs.python.org/install/index.html#inst-search-path
Probably terribly obvious buy tough stuff for an intro text!
Update
A few details for the record for anyone who comes across this later:
My installation does not have a file or folder "python2" so the pathname "/home/downey/swampy-2.0/python2" might not work. I just dropped the "python2" and all was well.
Had a bit of a problem finding the bashpc or bash_profile file. Didn't seem to exist even when looking for hidden files. So I just created a new one. All seems well.
Thanks again for your help--I would never have figured it out :)
I'm surprised there isn't some sort of installer or script to handle this given the audience for the book (newbies like me). :)
Open the terminal and navigate to the folder where you unzipped Swampy. Find the full directory path of that location by typing pwd. Let's say that location is /home/downey/swampy-2.0/python2. Now in the terminal issue this command export PYHTONPATH="/home/downey/swampy-2.0/python2". This will set the PYTHONPATH environment variable.
If that works fine, you need to make sure that this always runs when you open the terminal. For that you need to edit a file called .bashrc on your computer.
In the terminal, try nano ~/.bashrc.
That will open a simple text editor. In that file add the same line export PYHTONPATH="/home/downey/swampy-2.0/python2". Type Ctrl+X to close and make sure you save.
Now instead of always setting the environment variable manually, you can just do source ~/.bashrc and that will execute the command for you.
Note that every time you exit and reopen the terminal, you need to run source ~/.bashrc for the PYTHONPATH to be set.If you don’t want to run source ~/.bashrc every time you open the terminal, you can configure the Terminal to automatically run this command at startup (i.e. when the terminal window is opened). To do that –
Go to Terminal –> Preferences –> Settings –> Shell.
Check ‘Run Command’ and enter source ~/.bashrc.
NOTE: These settings are only need to be applied to each profile separately. For example if you applied these settings to the Basic profile, they won’t be applicable to Grass profile.
Hope this helps!
An easy way to add variables to your path is to add them to your .bash_profile, which is located in your home directory. This file loads every time you open up terminal. It is a hidden file, so you will not be able to view it (by default) in Finder. Run this command after opening a new terminal window:
open -a TextEdit .bash_profile
This will open the profile up in a new TextEdit window. Add this & save:
PYTHONPATH=${PYTHONPATH}:/home/downey/swampy-2.0/python2
export PYTHONPATH
Assuming your Swampy download path is the same from the Web Site (/home/downey/swampy-2.0/python2), the next time you run terminal, the path should now be included.

Categories

Resources