I've been told that to activate a Virtual Environment in Django for Windows I should try:
environment_path\Scripts\activate
But when I enter that, cmd returns this error:
The system cannot find the path specified.
I created the virtual environment by entering:
python3 -m venv env
However when I try env\Scripts\activate I get the error described.
Can anyone help? Thanks.
Activating a virtual environment on Windows depends on where you are running it from. From the Windows Command Prompt, the command is:
environment_path\Scripts\activate.bat
If the Git-Bash shell is being used, which adds several useful tools and a more Unix/Linux-like environment, then the command is:
source environment_path\Scripts\activate
I have found the prerequisites section of Test-Driven Development with Python aka "the Testing Goat book" to be handy in getting Django, Python and related things setup on Windows.
You can create virtual environment in windows as:
py -m venv myenv (Here myenv is the name that users give. It can be anything you want)
Now to activate your myenv virtual environment type:myenv\scripts\activate
To deactivate virtual environment simply type:
deactivate
If you are using windows 7, then try environment_path\Scripts\activate.ps1
Also, error is saying, The system cannot find the path specified., make sure your path is correct.
Remember scripts folder is in your virtualenv folder.
I want to learn Data Science and so have used some really popular Python modules likes Pandas, Matplotlib, Numpy, etc. So I clean installed Anaconda and am now using it as my default Python interpreter and also using Conda for installing packages and making virtual environments. I use VS Code as my daily text editor. But I have run into some issues when using the integrated Git terminal in VS Code with the Anaconda Python interpreter.
There are a couple of issues that I am facing. One of the first issues that I see is when I am using CMD to run Python. If I type and enter python in cmd, the Python interpreter provided by anaconda comes up. But I also get a warning:
Warning:
This Python interpreter is in a conda environment, but the environment has not been activated. Libraries may fail to load. To activate this environment please see https://conda.io/activation
I didn't expect to get this output. Anyway, there's another problem in VS code. But first I would like to mention that I have checked "Add to PATH" when installing Anaconda so no issues there. Now, when I open a new Terminal in VS Code, automatically C:/Users/User/Anaconda3/Scripts/activate is run and then conda activate base is run. But when conda activate base is run, automatically, as mentioned, I get a CommandNotFoundError. It states 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'
And then I am told to initialize my shell, so I did conda init bash but still no luck. And this brings me to talk about .bash_profile. I think it has to do something with this bash profile. Anyway, this is what is in my bash profile
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
eval "$('/C/Users/User/Anaconda3/Scripts/conda.exe' 'shell.bash' 'hook')"
# <<< conda initialize <<<
Just a summary of the problem:
Unexpected warning in CMD when running Anaconda Python interpreter
Automatically run Anaconda Scripts and conda activate base when opening new Terminal in VS Code
Conda init bash not helping
P.S I have tried using conda activate [env_name] in CMD and also in Git Bash and they work without any issues. In other words, Anaconda and Conda work perfectly outside of VS Code terminal.
I have figured out the answer myself and would like to share it here. First of all at the time of writing the question I was using Git Bash as my Terminal in VS Code (am still using it). So the issue was that when I ran the command conda init bash in Git Bash or the VS Code Terminal, Conda just basically put the command used for activating Conda environments in the .bash_profile since it is sourced during logging into Bash. But the integrated Terminal in VS Code is a subshell of a Git Bash session. That is why .bash_profile is NOT sourced in VS Code since .bash_profile is only sourced during the main Bash session. The .bashrc file is the file that is sourced when creating a Terminal session in VS Code. So what you actually need to do is take the code that is put into .bash_profile by conda init bash and paste it into your .bashrc file and make the .bash_profile source that .bashrc file automatically.
So, to sum up, using conda init bash will put the conda command in the .bash_profile and it is usually sourced by Git Bash, but VS Code Git Bash terminal will use .bashrc.
So you can just cut and paste the code from .bash_profile to .bashrc (as already mentioned) or if you want, just simply follow this: put this code in your .bash_profile:
if [ -f ~/.bashrc ]; then
source ~/.bashrc
fi
And in your .bashrc, put this code:
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
eval "$('{path_to_your_conda.exe}' 'shell.bash' 'hook')"
# <<< conda initialize <<<
# You can get conda.exe path by using `which conda` in Git Bash
Now, when you open up a Git Bash session in VS Code Terminal, you can use conda activate env_name to activate any environments you have.
Everything is now supposed to work as expected in VS Code terminal but I would like to further elaborate about something. If you want, you can skip the conda init bash process (NOT recommended, just read on for additional knowledge but please follow the above steps only). This is a feature that was introduced in conda 4.4.0. Till then the way of activating conda environments was by using source activate but that command was NOT cross-platform, meaning that the command could not be used in OSes like Windows.
So they made this change by introducing commands like: conda activate env_name so that conda environments become much easier to use despite the OS platform.
conda activate also has other advantages. This is directly from their release docs:
conda activate: The logic and mechanisms underlying environment activation have been reworked. With conda 4.4, conda activate and conda deactivate are now the preferred commands for activating and deactivating environments. You'll find they are much more snappy than the source activate and source deactivate commands from previous conda versions. The conda activate command also has advantages of (1) being universal across all OSes, shells, and platforms, and (2) not having path collisions with scripts from other packages like Python virtualenv's activate script.
I used this question as a reference. Check it out to learn more.
Having said that, using source activate env_name will still work if you are using Git Bash, even in VS Code Git Bash terminal. source activate env_name requires no prior set up or config. But it is highly recommended that you only use conda init to set everything up and then use conda activate env_name.
[NOTE]: Locating and modifying the said .bashrc and .bash_profile on Windows is usually not as simple as it is on Linux. But can be done fairly easily like this:
It goes without saying but, you should have Git Bash installed. Having Git Bash installed should, as far as I know, automatically create .bashrc or .bash_profile or maybe both. These files are called "dotfiles" (since they start with a dot) and these are by default hidden on most OSes and definitely on Windows. If they were auto-created by Git Bash on your system, it is most likely that they are placed in your home directory. Home directory on Windows is C:\Users\<you>\. With that said, follow this:
Open Git Bash and go to your home directory with: cd. Just type this and you will be in your home directory
Enter this command: ls -a and you will see all your files, even hidden ones. Look for .bash_profile and .bashrc. Both should be present. If they are, you are ready to follow the above instructions. But if one is not there or if both are missing create them using: touch .bashrc && touch .bash_profile. You should now see these files when you again type: ls -a
That's it. Now that you have your .bashrc and .bash_profile, you can follow the above instructions. Also, to access these two files quicker, open them like this with VS Code: code ~/.bashrc or code ~/.bash_profile. Now, modify these two files as per the instructions.
In the question, I have also talked about VS Code activating Conda environments automatically. There's no issue with VS Code doing that since this is the default behavior.
I misinterpreted that as something that's an issue. But if anyone was looking to stop VS Code from automatically doing that, I would recommend trying to set this in the user settings:
"python.terminal.activateEnvironment": false
EDIT: A better solution than using source activate to get conda activate commands to work in the git bash terminal in VS Code:
Run conda init in the Git Bash Terminal in VS Code
Type in bash -l in VS Code's Git Bash terminal to launch your configured shell as a login shell
You should now be able to run conda activate commands per normal!
More info: bash -l runs your ~/.profile/~/.bash_profile/~/.zprofile scripts where the conda executable is actually referenced (but in which Git Bash as a integrated terminal does not run by default and refers to). Hence, git bash does not know where to search for conda when running conda activate commands and per Arafat's explanation above, running conda init changes the git bash PATHs in this .bash_profile file, but is ineffectual as the git bash terminal in VS Code doesn't actually refer to this file! Further info in VS Code's official docs.
Supplementing the explanation of the accepted answer, I've posted a solution that worked for me here that might possibly help others (changing user settings did not solve the issue for me). Link could also point to other working solutions if the below or accepted answer above doesn't work.
NOTE: Please read Arafat's answer before attempting the source activate method below to understand why it's not normally recommended. That said leaving it up as it still solves the problem.
Here's what worked for me using the Git Bash terminal in VS Code on
windows in succinct steps:
source activate env-name - You should see your line appended by the (base) tag now.
After calling on source activate, I've found following conda activate commands to work: i.e. conda activate env2-name
What didn't work for Git Bash (as a VS Code terminal) for me: activate
env-name and conda activate env-name.
A year later I am still running into this issue. The following is a streamlined and updated approach based on Arafat's answer.
Install Git Bash
Configure Git Bash to be used in VSC (see How do I use Bash on Windows from the Visual Studio Code integrated terminal?)
Open the git bash Terminal from VSC
If conda activate is run successfully, skip the rest
run
conda init bash
Check for the exiting bash dot files:
ls -al ~/.bash*
Likely only one of '.bashrc' and '.bash_profile' exist
Check the existing dot file for conda initialization code
e.g.
cat ~/.bash_profile
This included in my case '>>> conda initialize >>> ...' code
(But, and this is the source of the problem, it is not executed when the terminal is opened. To check which of the files is executed simple add 'echo hello-X' to each of them.)
To fix the problem, we must create the missing dot file and make it execute the OTHER previously existing one:
tee -a ~/.bashrc << END
if [ -f ~/.bash_profile ]; then
source ~/.bash_profile
fi
END
Reopen the Terminal in VSC
I have installed an additional Anaconda environment, running python 3.5, so that now I have:
# conda environments:
#
python3.5 /Users/user/anaconda/envs/python3.5
root * /Users/user/anaconda
The Anaconda documentation says that I need to do source activate python3.5 to activate the new environment. What it does not mention is that activate only works in bash. I'm using tcsh, so that I currently have to switch to bash, issue the command above, and switch back to tcsh every time I open a new shell.
How can I ease this process?
If you really want to stay inside this shell, you need to replicate the logic that the activate script does for bash in your shell. I have found a gist that might work:
https://gist.github.com/mikecharles/f09486e884a0b41e1e8f
I attempted the following procedures to setup a conda environment for arcgis. The environment set-up correctly and I was prompted to activate the environment by typing "activate esri101" to which I was prompted...
$ activate esri101
sh.exe": activate: command not found
I'm a 3 out of 10 on the newbie to expert scale. I've tried setting my path as specified here to no avail. I don't know where exactly to go from here.
EDIT: I retried it again today and it seemed to work
On a Linux/MacOSX environment, you must type (Documentation here)
$ source activate esri101
that is the Linux/MacOSX equivalent to the
activate esri101
that you would use on Windows
On my mac terminal, I had to use:
conda activate env_name
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.