Ben
Ben

Reputation: 13352

Make VS Code terminal match debug environment on a Mac

I'm teaching a beginners python class, the environment is Anaconda, VS Code and git (plus a few extras from a requirements.txt).

For the windows students this runs perfectly, however the mac students have an existing python (2.7) to contend with.

The windows students (i.e. they have a windows computer), their environment when they debug matches their console environment. However the mac students seem to be locked to their 2.7 environment.

I've tried aliasing, as suggested here and here

alias python2='python'
alias python='python3'
alias pip2='pip'
alias pip='pip3'

I've modified the .bash_profile file

echo 'export PATH="/Users/$USER/anaconda3/bin:$PATH"' >>.bash_profile

Both of these seem to work perfectly to modify their Terminal environments, when launched externally to VS Code. Neither seem to do anything to the environment launched from [cmd]+[`].

I've also tried conda activate base in the terminal, which seems to have no effect on a python --version or a which python

They can run things using python 3, but that means that they need to remember that they are different to the other 2/3 of the students. It's othering for them, and more work for me!

The students are doing fine, launching things from their external terminal, but it would streamline things greatly if the environments could be as consistent as possible across the OSs.

Whilst they are complete beginners, they can run a shell script. They currently have one that installs pip requirements and vs code extensions.

Is there a configuration that will keep the terminal in line with the debug env?

Upvotes: 2

Views: 3747

Answers (3)

igrinis
igrinis

Reputation: 13636

In my opinion the best practice is to create Python virtual environments (personally I love using conda environments, especially on Mac where you stuck with unremovable old Python version). Then VSCode will automatically (after installing very powerful Python extension) find all your virtual environments. This way you will teach your students a good practice of handling Python zoo a.k.a. package incompatibilities. Terminal environments settings will be consistent with VSCode, without being dependent on unneeded any more aliases. Obviously, virtual environments are OS independent, so you will be more consistent and remove unnecessary confusion between different students.

The additional bonus of the virtenvs is that you can create one exactly according to your requirements.txt and switch from one to another with a single click (in terminal it takes two commands: deactivate -> activate).

You can read more about how to handle Python virtual environments on VSCode site

Upvotes: 4

Zubda
Zubda

Reputation: 963

Python in vscode let's you select which interpreter will be used to run the scripts.
It is in settings under "python.pythonPath", just set it to point to the interpreter of choice.
It can be set on a project basis as well (which is how you ensure that a project that has a virtual environment will execute using that interpreter and packages), you just select Workspace in the settings pane and add the desired python interpreter there.

Upvotes: 3

ishaanv
ishaanv

Reputation: 152

Given the aliases are run just once and are not persistent in .bash_profile, python targets the default interpreter rather than the expected conda python3 interpreter.

Try to symlink conda's python3 executable to capture the python namespace

ln -sf /Users/$USER/anaconda3/bin/python3 /Users/$USER/anaconda3/bin/python

This will create or update the symlink. Use the same approach for pip and pip3.

Upvotes: 3

Related Questions