How to get vscode to use `conda activate` instead of `source activate`? - visual-studio-code

When opening a new terminal in vscode, if I have
"python.terminal.activateEnvironment": true
and I already selected a Python interpreter (see related question here and documentation here), then the terminal opens and automatically activates the environment using
source /path/to/the/environment/bin/activate /path/to/the/environment
I can turn this behavior off by setting python.terminal.activateEnvironment to false.
Of course, I can also manually activate the environment using
conda activate /path/to/the/environment
How to get vscode to use conda activate automatically instead of source activate?
Using source as vscode does sets the activated environment as the "base", e.g. if I do conda env list I will see something like:
conda env list
# conda environments:
#
/path/to/the/actual/base/environment
base * /path/to/the/environment
and this causes some issues. For example, if I try to use mamba which I have in my "true" base environment but not in the other one, it will cause a "No such file or directory" error.

Related

Can't run `terminal.integrated.env` command in integrated terminal: "command not found"

Following this manual page:
https://code.visualstudio.com/docs/python/environments
I would have to set PYTHONPATH "through the terminal settings":
The PYTHONPATH environment variable specifies additional locations
where the Python interpreter should look for modules. In VS Code,
PYTHONPATH can be set through the terminal settings
(terminal.integrated.env.*) and/or within an .env file.
How do I do that? If I write (I ssh to a Linux server):
terminal.integrated.env.linux
I get "command not found".
It seems you tried to enter this
terminal.integrated.env.linux
into the terminal prompt itself, which treated it as a literal command.
What that guide on PYTHONPATH meant when it said "through the terminal settings" was Visual Studio Code's Integrated Terminal settings: https://code.visualstudio.com/docs/getstarted/settings. Specifically the settings under Terminal (filtered on terminal.integrated.env):
Depending on your OS/platform, you put a terminal.integrated.env.<os> block in your VS Code's settings.json file to specify the environment variables to inject when using VS Code's integrated terminal:
From https://code.visualstudio.com/docs/getstarted/settings#_default-settings:
// Object with environment variables that will be added to the
// VS Code process to be used by the terminal on Linux. Set to
// `null` to delete the environment variable.
"terminal.integrated.env.linux": {},
So if you are on Linux:
"terminal.integrated.env.linux": {
"PYTHONPATH": "<absolute path>"
},
Note: see the Python docs on PYTHONPATH on what exactly needs to be specified here.
Or, as the guide says, you can also specify a .env file.

How to use Python3 on the VScode terminal?

Is there a way to force VS Code to use only python3? It always defaults to python2.7 no matter what I try. I've tried selecting the correct interpreter as python3.7. When I open up terminal, it immediately uses python2.7, In the settings it is pointing at 3.7, but the built in terminal which is nice, always defaults to 2.7.
First, understand that the integrated terminal of VSCode, by default, uses the same environment as the Terminal app on Mac.
The shell used defaults to $SHELL on Linux and macOS, PowerShell on
Windows 10 and cmd.exe on earlier versions of Windows. These can be
overridden manually by setting terminal.integrated.shell.* in user
settings.
The default $SHELL on Mac is /bin/bash which uses python for Python2.7. So VS Code will just use the same python to mean Python2.7. When you open a bash shell, it will load your ~/.bash_profile to apply custom aliases and other configurations you added into it.
One solution to your problem is edit your ~/.bash_profile to alias python to python3. But I do not recommend this because this affects all your bash sessions, even those outside of VS Code. This can lead to nasty side effects when you run scripts that need python to be the system Python2.7.
You can instead configure VSCode to load its own aliases, for its own integrated terminal. First, create a file named vscode.bash_profile in your home directory:
$ cat ~/vscode.bash_profile
alias python=$(which python3)
On my env, python3 is Python3.7. You can set it to the what's applicable on your env (ex. maybe python3.7). Then, in VS Code, look for the Terminal shell args setting:
and then open your settings.json and add these lines:
"terminal.integrated.shellArgs.osx": [
"--init-file",
"~/vscode.bash_profile",
]
Finally, restart VS Code. The next time you open the VS Code terminal, python should now be using your Python 3 installation. This should not affect your bash session outside of VS Code.
Note that, if you have some custom settings from the default ~/.bash_profile, you may want to copy it over to your ~/vscode.bash_profile, so that you can still use it on VS Code (ex. changes to PATH, git-completion scripts..).

I was messing with Visual Studio Code and now all terminals open with (base) environment

As the title says I was messing with Visual Studio Code while I was checking the debugging functionality.
I obviously did something wrong because since then every new terminal I open, both on VSC and Ubuntu terminal opens with (base) environment
Any ideas how to fix it?
Somewhere you have run conda init, which adds an entry to your .bashrc file to automatically activate the base environment whenever you start a bash terminal.
If you want to undo this change completely, you can run:
conda init --reverse
However, this will mean that you can't run conda activate, for example.
If you just want to prevent conda from activating the base environment when you start your shell session, you can run:
conda config --set auto_activate_base false
This will create a .condarc file in your home directory, with an entry of auto_activate_base: false. This will override any conda defaults.

How to automatically set environment variable for eclipse terminal view?

I use the terminal view in eclipse:
Every time I start the terminal I run a command to set an environment variable, e.g.
SET HTTPS_PROXY=***.**.***.***:****
I need this as a prerequisite to be able to download some python packages with the python package manager, e.g
pip install mesa
=> Is there a way to automatically run a "startup command" when the Eclipse terminal is opened?
I expected that there would be some Eclipse setting to configure the environment variables for the terminal or to specify some initialization script. However, I could not find such a setting.
I tried to set the proxy settings in eclipse.ini
-Dhttp.proxyHost=***.**.***.***
-Dhttp.proxyPort=****
-Dhttp.nonProxyHosts=localhost|127.0.0.1
However, that does not seem to influence the proxy for the terminal view.
(I actually use a portable version of eclipse with EclipseProtable and in EclipsePortable.ini I am already able to specify some extra paths that will be known when I open a terminal view, e.g.
AdditionalPaths=./App/WinPython/python-3.7.2.amd64;
Also see https://portableapps.com/node/32861 for informatino about EclipsePortable
However, I was not able to set envirionment variables in the ini file EclipsePortable.ini.
)
You can use your system environment variable.
In Windows search the "User Account" parameter.
It must show your current account, search for "Environment variable".
Now you can set all the variables you need and they will be used by default in any terminal in Eclipse (restart Eclipse if it was running).

How to set a default environment for Anaconda / Jupyter?

I have installed Anaconda for my Machine Learning course. I'm using it as IPython (Jupyter) notebook, in which we have lessons. OS is Ubuntu 14.04 LTS. Basically, I always run it from Terminal with:
jupyter notebook
I have created new environment called su_env from root environment (exact copy) with one package added. Now, I'm wondering: how can I set environment su_env as default one? I have dozen of notebooks so it's annoying to set up each time for every notebook the environment, in "web" GUI of Jupyter.
EDIT: I'm interested in a solution where you don't have to set environment before running notebook. My logic is that, somehow, automagically, jupyter sets root environment on its own while starting up. Because of that, I'm wondering is it possible to set some config file or something so jupyter sets su_env instead of root. Also, if you know that's not possible (and why), I would like to know that.
First activate the conda environment from the command line, then launch the notebook server.
For example:
$ source activate env_name
$ jupyter notebook
Note: This might only work with environments that were created from within Jupyter Notebook, not environments that were created using conda create on the command line.
In your ~/.bashrc, include the line:
alias jupyter="source activate su_env; jupyter"
This will condense the two commands into one, and you will activate su env whenever you call jupyter notebook or lab or whatever
Edit your bashrc and add source activate su_env then that env will always be active. To switch back to to root (or any other env) source activate env_name
You can use this on conda prompt:
conda activate env_name
jupyter notebook
source activate env_name gives me an error: 'source' is not recognized as an internal or external command, operable program, or batch file.