Virtual Environment Not showing up in VS Code Terminal - python

I was trying to activate a Conda environment from VS Code Terminal. Everything worked but it does not show that it is using the specified Conda Environment. Take a look at my Terminal.Just PS(no venv)
But when I looked at other people's terminal. It is actually showing their environment name in the bracket. Just on a Curious Note, I am asking Why it is so?
virtual environment name in bracket

The reason is that "PowerShell" does not activate the conda environment by default.
Solution:
Please use the command
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process
in terminal to change the execution policy of "PowerShell", and then open a new VS Code terminal (Ctrl+Shift+`):
Check: We can view that the "profile.ps1" file is generated here.
Reference: Conda environments in VS Code.

Related

Virtualenv activating and deactivating on windows

I have been able to create a virtual environment on a Windows machine by following the docs. I am very familiar with virtualenv but on macos.
However when I try to activate the environment:
Scripts/activate.bat as described in the docs
I see no action that shows it has been activated. I do see time pass before the bash resets ready for another command so thats something.
But when I type deactivate to deactivate the virtual environment I get
bash: deactivate: command not found
So either it is being activated and I am not correctly deactivating (even though the docs says just to type deactivate much like mac).
Or nothing is being activated.
Anyone familiar?
The .bat files are for the Windows command shell, cmd.exe. If you're using bash on Windows, you should use the corresponding (ba)sh scripts, for example source tutorial-env/bin/activate.

Terminal on VSCode is not grabbing the right python or ipython

If I'm using a regular Terminal with zsh, I can start any conda environment and the python or ipython commands open the "right" version (on a particular conda env it is Python 3.7.7.).
But if I'm on a Terminal under VSCode, I always get a different version, as if it was not assuming the conda environment, although it the prompt shows me the correct conda env.
Any idea on what might be the problem here?
In .vscode/settings.json, you can put the python version required :
{
"python.pythonPath": "/usr/bin/python3"
}
In VS Code, its internal terminal is to integrate the terminal from the system, and it defaults to use the python environment selected in the lower left corner of VS Code when it was created. Therefore, please use the shortcut key Ctrl+Shift+` to open a new VS Code terminal, it will Automatically enter the selected python environment, and for the selected conda environment, it will automatically activate it:
Also, please try to reload VS Code.
Reference: Conda environments in VS Code.
Leaving here the answer as I just found it is a known bug.
https://github.com/microsoft/vscode-python/issues/5764
This happens for non-Windows machine and if the selected interpreter is a conda environment and "terminal.integrated.inheritEnv" is not set. Solution is to set "terminal.integrated.inheritEnv" to false.

How to activate virtual environment in Vscode when running scripts are disabled in system?

I created a virtual environment in vscode in a folder called server by typing:
python -m venv env
And I opened the server folder, select interpreter Python 3.8.1 64-bit('env':venv)
then I got following error:
I can't find any solution to this and I am stuck for hours.
It seems that it is going to activate the environment through a powershell script. And running such scripts is turned off by default. Also, usually a virtual environment is activated through cmd and .bat script. You could either turn on running powershell script or make VS Code activate an environment through cmd and .bat file.
The first way - using cmd instead of Powershell
I just checked it in my PC and VS Code doesn't use Powershell at all. It activate an environment with cmd instead of Powershell. Probably it is worth to check VS Code settings, set cmd as a default terminal. It is probably such an option in the main settings.json (you can open it through ctrl+shift+p and type 'open settings (JSON)'): "terminal.integrated.shell.windows": "C:\\Windows\\System32\\cmd.exe",.
The second way - changing Powershell execution policy
In order to change Powershell execution policy you can add "terminal.integrated.shellArgs.windows": ["-ExecutionPolicy", "Bypass"] to your main VS Code settings.
Also you can open a Powershell window as administrator and type the following:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
Then respond y to any questions.
An update with regards to using the Command Prompt instead of Powershell in VS Code:
When implementing "terminal.integrated.shell.windows": "C:\\Windows\\System32\\cmd.exe" in settings.json it gives the notice:
This is deprecated, the new recommended way to configure your default shell is by creating a terminal profile in #terminal.integrated.profiles.windows# and setting its profile name as the default in #terminal.integrated.defaultProfile.windows#. This will currently take priority over the new profiles settings but that will change in the future
After taking a look at the documentation I've found that the correct alternative would be to include a line as shown below.
"terminal.integrated.defaultProfile.windows": "Command Prompt"
Alternatively, you can also use the GUI as shown in the documentation. More advanced settings are also shown there.
Try Using Cmd in vscode and Run this command env(your Virtual Env name)\Scripts\activate

VSCode conda activate base giving CommandNotFoundError

I have Anaconda and Visual Studio Code installed on my computer. My default terminal for VS Code is Git Bash. When I open a new terminal in VSCode, it immediately runs the following commands:
C:/Users/ethan/AppData/Local/Continuum/anaconda3/Scripts/activate
conda activate base
The second of these commands gives the following error:
CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
If using 'conda activate' from a batch script, change your
invocation to 'CALL conda.bat activate'.
I have tried running conda init bash and conda init --all both inside the VSCode terminal, and inside Git Bash. It handles that command fine, but it doesn't solve my problem. I don't know if the second line of the error applies to me, but even if it did, I don't know how to change the command being called because it is done automatically by VSCode. This error occurs every time I launch a terminal in VSCode (even if I don't have any python files present in my workspace), and it happens both when I launch VSCode from the launch button in Anaconda Navigator and when I launch VSCode by itself.
I had the same issue. For me, easily resolved by launching VSC from the conda window.
Specifically, open your cmd prompt (for me, Anaconda Prompt), activate the environment using 'conda activate [envname]'. Then just run the command 'code'. This will launch VS Code with the activated environment and associated variables. From there, the debug works as expected.
I had the same issue, I've fixed it by adding the Python.CondaPath in settings.
Press Ctrl + Shift + P and select Terminal Configuration. Search for python.conda, and paste your conda path for example. C:\ProgramData\Anaconda3\Scripts\conda.exe
This will fix your issue.
In VS code settings, search for "terminal.integrated.shellArgs.windows", then click "Edit in settings.json". For me, this opened "%APPDATA%\Code\User\settings.json".
I set "terminal.integrated.shellArgs.windows": "-i -l" and this fixed it for me. My file:
{
"terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe",
"terminal.integrated.shellArgs.windows": "-i -l"
}
Since conda activate command would cause CommandNotFoundError, use source activate command instead. It works the same.
I had the same issue. I got the following error:
CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
If using 'conda activate' from a batch script, change your invocation to 'CALL conda.bat activate'.
To initialize your shell, run
$ conda init <SHELL_NAME>
Currently supported shells are:
- bash
- cmd.exe
- fish
- tcsh
- xonsh
- zsh
- powershell
See 'conda init --help' for more information and options.
IMPORTANT: You may need to close and restart your shell after running 'conda init'.
I solved it by manully run
source activate base
and Ta-da~ DONE!
This trick is simple, while needed every time you open a new git-bash terminal in VSCode.
This worked for me go to setting and search for
terminal.integrated.shellargs.windows
then choose Command Prompt like this
I had exactly the same error as you. I solved it with a tip from a Python course in Udacity
Open Git Bash command line (NOT within VSC terminal) and go to your home folder, e.g., /c/Users/arman. Then run the following two commands but replace [YOUR_PATH] with your Anaconda installation folder
echo 'export PATH="$PATH:[YOUR_PATH]:[YOUR_PATH]/Scripts"' >> .bashrc
echo 'alias python="winpty python.exe"' >> .bashrc
For example in my case, as I have miniconda, I executed:
echo 'export PATH="$PATH:/c/Users/arman/Miniconda3:/c/Users/arman/Miniconda3/Scripts"' >> .bashrc
echo 'alias python="winpty python.exe"' >> .bashrc
After this executing those lines, i.e., creating the .bashrc file, then run:
source .bashrc
Afterwards, open VSC and try running or debugging a python program. It worked for me!
I solved this issue by using Powershell. Start the Powershell as Administrator and then type
set-ExecutionPolicy RemoteSigned
Say yes if it asks a confirmation. Now, VSCode debugger option can be used with Python.
When I changed my default terminal in VS Code to cmd.exe I got conda to work properly for me.
None of these worked for me. In the end I changed my default VScode terminal to cmd instead of Powershell in the terminal default settings which was None for Windows. I had already added conda.exe to my path and changed the permissions as described above.
For some reason the activate.bat file was now found in the path which activated my virtual env.
This answer is dedicated to Windows 10/11 users, based on PowerShell VS Code integrated terminal, and assumes using miniconda but the same holds for conda under the previous environment.
From the start menu type or search for Anaconda Powershell Prompt then right-click to open its file location. You should see the PowerShell shortcut. Right-click and open properties. In the target bar try to find C:\Users\username\miniconda3\shell\condabin\conda-hook.ps1' ; PowerShell script (.ps1). Using file explorer locate C:\Users\username\miniconda3\shell\condabin. Copy the content of that script.
In the default Documents folder create a folder named WindowsPowerShell if not exists and edit or create a new Microsoft.VSCode_profile.ps1 script in it. Lastly, paste by appending the content of the former script.
You could do the same for the external PowerShell but the script under the path C:\Users\username\Documents\WindowsPowerShell should be named Microsoft.PowerShell_profile.ps1.
In other words, the PowerShell profile script enables the creation of aliases and defining functions that load every time you launch the shell. However, the changes take effect after restarting VS Code and/or PowerShell.

How to change default Anaconda python environment

I've installed Anaconda and created two extra environments: py3k (which holds Python 3.3) and py34 (which holds Python 3.4). Besides those, I have a default environment named 'root' which the Anaconda installer created by default and which holds Python 2.7. This last one is the default, whenever I launch 'ipython' from the terminal it gives me version 2.7. In order to work with Python 3.4, I need to issue the commands (in the shell)
source activate py34
ipython
which change the default environment to Python 3.4. This works fine, but it's annoying since most of the time I work on Python 3.4, instead of Python 2.7 (which I hold for teaching purposes, it's a rather long story). Anyway, I'll like to know how to change the default environment to Python 3.4, bearing in mind that I don't want to reinstall everything from scratch.
If you just want to temporarily change to another environment, use
source activate environment-name
ETA: This may be deprecated. I believe the current correct command is:
source conda activate environment-name
(you can create environment-name with conda create)
To change permanently, there is no method except creating a startup script that runs the above code.
Typically it's best to just create new environments. However, if you really want to change the Python version in the default environment, you can do so as follows:
First, make sure you have the latest version of conda by running
conda update conda
Then run
conda install python=3.5
This will attempt to update all your packages in your root environment to Python 3 versions. If it is not possible (e.g., because some package is not built for Python 3.5), it will give you an error message indicating which package(s) caused the issue.
If you installed packages with pip, you'll have to reinstall them.
Overview
Some people have multiple Conda environments with different versions of Python for compatibility reasons. In this case, you should activate the desired default environment in the shell initialization file (e.g., .bashrc, .zshrc). With this method, you can preserve the versions of Python you use in your environments.
The following assumes environment_name is the name of your environment
Mac / Linux:
Edit your bash profile so that the last line is conda activate environment_name. In Mac OSX this is ~/.bash_profile, in other environments this may be ~/.bashrc
Example:
Here's how I did it on Mac OSX
Open Terminal and type:
nano ~/.bash_profile
Go to end of file and type the following, where "p3.5" is my environment:
conda activate p3.5
Exit File. Start a new terminal window.
Type the following to see what environment is active
conda info -e
The result shows that I'm using my p3.5 environment by default.
For Windows:
Create a command file (.cmd) with activate environment_name and follow these instructions to have it execute whenever you open a command prompt
Create a batch file command, e.g. "my_conda.cmd", put it in the Application Data folder.
Configure it to be started automatically whenever you open cmd. This setting is in Registry:
key: HKCU\SOFTWARE\Microsoft\Command Processor
value: AutoRun
type: REG_EXPAND_SZ
data: "%AppData%\my_conda.cmd"
from this answer: https://superuser.com/a/302553/143794
Under Linux there is an easier way to set the default environment by modifying ~/.bashrc or ~/.bash_profile
At the end you'll find something like
# added by Anaconda 2.1.0 installer
export PATH="~/anaconda/bin:$PATH"
Replace it with
# set python3 as default
export PATH="~/anaconda/envs/python3/bin:$PATH"
and thats all there is to it.
For windows Anaconda comes with Anaconda Prompt which is a shortcut to cmd and can be used run conda commands without adding anaconda in PATH variable.
Find the location of it, copy and rename the copy (say myenv_prompt). Right click myenv_prompt and select properties in the context menu.
The Target form of Properties window should already be filled with text, something like %windir%\system32\cmd.exe "/K" C:\Users\xxx\AppData\Local\Continuum\Miniconda3\Scripts\activate.bat C:\Users\xxx\AppData\Local\Continuum\Miniconda3\
There are three parts of this command 1)start ...\cmd.exe 2)run ...\acitvate.bat with environment 3)...\Miniconda3\
Change 3rd part to path of the environment (say myenv) you want as default i.e. fill the Target form something like %windir%\system32\cmd.exe "/K" C:\Users\xxx\AppData\Local\Continuum\Miniconda3\Scripts\activate.bat C:\Users\xxx\AppData\Local\Continuum\Miniconda3\envs\myenv
Now myenv_prompt will act as shortcut to start cmd with myenv as the default environment for python. This shortcut you can keep in start menu or pinned in taskbar.
One advantage of this method is that you can create a few shortcuts each having different environment as default environment. Also you can set the default folder by filling Start in form of the Properties window
Hope this helps
PS:It is not required to find Anaconda Prompt and can be done by changing target of any shortcut. But you will require to know path of cmd.exe and activate.bat
Just activate your py34 environment when you load your terminal/shell.
If you use Bash, put the line:
conda activate py34
in your .bash_profile (or .bashrc):
$ echo 'conda activate py34' >> ~/.bash_profile
Every time you run a new terminal, conda environment py34 will be loaded.
The correct answer (as of Dec 2018) is... you can't. Upgrading conda install python=3.6 may work, but it might not if you have packages that are necessary, but cannot be uninstalled.
Anaconda uses a default environment named base and you cannot create a new (e.g. python 3.6) environment with the same name. This is intentional. If you want your base Anaconda to be python 3.6, the right way to do this is to install Anaconda for python 3.6. As a package manager, the goal of Anaconda is to make different environments encapsulated, hence why you must source activate into them and why you can't just quietly switch the base package at will as this could lead to many issues on production systems.
Change permanent
conda install python={version}
Change Temporarily
View your environments
run conda info --envs on your terminal window or an Anconda Prompt
If It doesn't show environment that you want to install
run conda create -n py36 python=3.6 anaconda for python 3.6 change version as your prefer
Activating an environment (use Anaconda prompt)
run activate envnme envnme you can find by this commandconda info --envs as a example when you run conda info --envs it show
base * C:\Users\DulangaHeshan\Anaconda3
py36 C:\Users\DulangaHeshan\Anaconda3\envs\py36
then run activate py36
to check run python --version
In Windows, it is good practice to deactivate one environment before activating another.
https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html?highlight=deactivate%20environment
If you want Anaconda Navigator to default to Virtual Env you created, go to file > Preference and select default conda env in drop down lint:
If you want Anaconda command automatically opens to virtual env without having to type activate envName, do this:
Right click on conda shortcut > go to properties and change the Target to something like this:
%windir%\System32\cmd.exe "/K" C:\Anaconda\Scripts\activate.bat C:\Anaconda\envs\p37
Optionally you can set your default working dir as well, like I did in snapshop below:
gl
On Windows, create a batch file with the following line in it:
start cmd /k "C:\Anaconda3\Scripts\activate.bat C:\Anaconda3 & activate env"
The first path contained in quotes is the path to the activate.bat file in the Anaconda installation. The path on your system might be different. The name following the activate command of course should be your desired environment name.
Then run the batch file when you need to open an Anaconda prompt.
Here is the solution I found for autoactivating my preferred environment on a Windows 10 system:
Open anaconda prompt & use 'conda env list' to find the location of the environment you wish to use.
Go to the start menu, right-click 'Anaconda Prompt' and go to file location.
Create a copy of this shortcut file
Open its properties & change the target to the location of your preferred environment.
Now every time you open anaconda prompt through this shortcut it will automatically load your chosen environment.
activate.py is hardcoded to emit conda activate base\n into your shell profile when you evaluate the shell hook produced by conda shell.zsh hook.
you can suppress this hardcoded "auto-activate base" via:
conda config --set auto_activate_base false
then, in ~/.zshrc, ~/.bashrc or wherever you source your shell profile from, you can append the following (after the conda shell hook) to explicitly activate the environment of your choosing:
conda activate py34
I wasn't satisfied with any of the answers presented here, since activating an environment takes a few seconds on my platform (for whatever reason)
I modified my path variable so that the environment I want as default has priority over the actual default.
In my case I used the following commands to accomplish that for the environment "py35":
setx PATH "%userprofile%\Anaconda3\envs\py35\;%PATH%"
setx PATH "%userprofile%\Anaconda3\envs\py35\Scripts;%PATH%"
to find out where your environment is stored, activate it and enter where python.
I'm not sure yet if this approach has any downsides. Since it also changes the default path of the conda executable. If that should be the case, please comment.
For Jupyter and Windows users, you can change the Target path in your Jupyter Notebook (anaconda3) shortcut from C:\Users\<YourUserName>\anaconda3 to C:\Users\<YourUserName>\anaconda3\envs\<YourEnvironmentName>
you could do the same thing for the Anaconda Prompt..etc.
After changing the path you can check your active environment by opening a terminal in Jupyter and run conda info --envs.
I got this when installing a library using anaconda. My version went from Python 3.* to 2.7 and a lot of my stuff stopped working.
The best solution I found was to first see the most recent version available:
conda search python
Then update to the version you want:
conda install python=3.*.*
Source: http://chris35wills.github.io/conda_python_version/
Other helpful commands:
conda info
python --version
Create a shortcut of anaconda prompt onto desktop or taskbar, and then in the properties of that shortcut make sure u modify the last path in "Target:" to the path of ur environment:
C:\Users\BenBouali\Anaconda3\ WILL CHANGE INTO
C:\Users\BenBouali\Anaconda3\envs\tensorflow-gpu
preview
and this way u can use that shortcut to open a certain environment when clicking it, you can add it to ur path too and now you'll be able to run it from windows run box by just typing in the name of the shortcut.
Tried both source activate default_3_9 and source conda activate default_3_9
but worked conda activate default_3_9
I'm trying to update Anaconda in order to use Python 3.10.4 and then Spyder 5.3.2. Actually, I wanted to set the Python interpreter used by Pycharm inside the Spyder console but it required the newest Spyder version. I didn't try all the possible solutions (it's pending for me to use the window batch and modifying path solutions given here) but:
Since I couldn't update the Anaconda base due to the well-known error on the "Solving environment". Then Python and Spyder remain the same.
Creating a new env allows to get the last Python and then his newest Spyder version but it doesn't actualize the Anaconda shortcuts and even the Anaconda navigator if you set it to this new env still has some inconsistencies like keeping the older Spyder version in his menu.
Besides, on point 2, changing the shortcuts target path doesn't work for me.
Finally, I create a new shortcut of the Spyder file from the Scripts folder inside the environment directory ( C:\Users<userName>>\Anaconda3\envs<EnvName>\Scripts )
I couldn't use the default Anaconda shortcuts but I have what I wanted and quick access.

Categories