virtualenv activate does not work - python

I am trying to create a virtual environment to test an api.
I can create the environment just fine using virtualenv test, then I can cd into it. When I try to run activate, I get this error:
PS C:\Users\Bright Bridge\Desktop\autocomplete_demo\Scripts> activate
activate : The term 'activate' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if
a path was included, verify that the path is correct and try again.
At line:1 char:1
+ activate
+ ~~~~~~~~
+ CategoryInfo : ObjectNotFound: (activate:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Suggestion [3,General]: The command activate was not found, but does exist in the current location. Windows PowerShell does not load commands from the current
location by default. If you trust this command, instead type: ".\activate". See "get-help about_Command_Precedence" for more details.
I recently updated windows, and it replaced the cmd with Windows Powershell. My virtualenv worked fine with the cmd, so I suspect it may have to do with the way the Powershell does things.
How can I activate the virtualenv?

If you press WindowsKey + R, and type cmd when the box comes up, it should bring up the command line interface instead of powershell!

For those of you still interested, the output gives you the answer:
If you trust this command, instead type: ".\activate".
So use a relative path name to run the activate script in powershell, or use an absolute path

You need to set windows execution policies. According to official documentation of virtualenv.
Run in powershell Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUse
Now you can activate virtual env. using \Scripts\Activate.ps1
Hope it solves it!

Just Enter one PowerShell Command
.\Scripts\activate
Rather Then
./Scripts/activate

Execute .\activate instead of activate
That worked for me.

You basically needs to set execution policy for PowerShell
Step 1: Run PowerShell as Administartor
Step 2: Run Below Command
Set-ExecutionPolicy RemoteSigned
After successfull run, You will be able to activate vertual environemnt.
Enjoy :)

after jumping to Scripts folder
cd Scripts
just write .\activate
It works perfectly

On my windows I use this way with gitbash
cd .myenv/Scripts
then navigate and type
activate
works for me

first make virtualenv with virtualenv pymote_env
Activate environment: pymote_env\Scripts\activate
in your case:-
first make virtualenv with virtualenv test
Activate environment: test\Scripts\activate
http://pymote.readthedocs.io/en/latest/install/windows_virtualenv.html

Use command pompt instead of using power-shell
and run the same command
this should work

Instead of all variants it works with C:...venv\scripts .\activate.ps1
But before that, I changed my ExecutionPolicy to RemoteSigned. You can do it by next steps.
Enter Set-ExecutionPolicy
Enter RemoteSigned
System should ask you if u wish to save changes. Answer with "Y"
enter image description here

1.Just add the complete path in cmd like this " cd.\pyenvDjango\Scripts" and press enter,
Now you see the complete directory path in which you are.
Now just type ".\activate".
this will activate your env.
NOTE:- try this in cmd not in vs code terminal or any where

Related

How do I activate my virtual environment using PyCharm? Can I even activate it in PyCharm?

I have am slightly confused about the local and command prompt in the terminal section of PyCharm. Could someone explain to me what the differences are?
Also, how do i activate my virtual environment from PyCharm, as i need it to run selenium (i placed it in a separate virtual environment). Do I use command prompt or local. I tried using local and when i type activate.bat in my Scripts folder, I come across this error message:
activate.bat : The term 'activate.bat' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
activate.bat
+ CategoryInfo : ObjectNotFound: (activate.bat:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Suggestion [3,General]: The command activate.bat was not found, but does exist in the current location. Windows PowerShell does not load commands from the current location by default. If you trust this command, instead type: ".\activat
e.bat". See "get-help about_Command_Precedence" for more details.
PS C:\Users\Jonathan\venvs\automation\Scripts>
Can someone assist me on how i can activate my virtual enviornment via PyCharm?
Yes, you can activate your virtual environment in Pycharm. Pycharm configures an interpreter for each project, and by default uses the system Python interpreter if not otherwise specified. To use the virtual environment's interpreter do the following:
Settings > Project > Python Interpreter
Click on the cog next to the path, and select Add... to add a new interpreter
Select existing environment, and enter the path of the venv
Click OK
After doing this Pycharm will do some indexing, and the next time you run your program it should run using that virtual environment.

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

Virtual Environment won't activate on Windows 10

I use Windows 10, python 3.7.4 and i've installed virtualenv with pip.
When i try to create virtual environment in cmd i can create with virtualenv myenv.It creates the files needed, Scripts folder and activate files are all there. But when i try to activate nothing happens. I try:
C:\Users\Spike\pyenv>cd crawler
C:\Users\Spike\pyenv\crawler>cd scripts
C:\Users\Spike\pyenv\crawler\Scripts>activate
After this virtual environment name should be in parenthesis like this:
(crawler) C:\Users\Spike\pyenv\crawler\Scripts
but it is not, virtual env is not activated, it continues like this:
C:\Users\Spike\pyenv\crawler\Scripts>
I've tried running cmd in admin mode, doesn't work.
I've tried to activate from Powershell:
PS C:\Users\Spike\pyenv\crawler> cd scripts
PS C:\Users\Spike\pyenv\crawler\scripts> activate
But i get a error:
activate : The term 'activate' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ activate
+ ~~~~~~~~
+ CategoryInfo : ObjectNotFound: (activate:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
So i've tried Set-ExecutionPolicy AllSigned and Set-ExecutionPolicy Unrestricted -Force on Powershell before activating it, it also doesn't work.
I've also tried to activate it in cmder, i get the same result i got in cmd.
To solve i first looked in to other stackoverflow thread but none helped.
Check the name of your virtualenv. You said the name of your env was myenv. However, you are in the pyenv directory. If you CD to the folder you where in when you created myenv, the following should work.
myenv/Sripts/activate
If that does not work, make sure the activate file is actually there.
Also, note that in certain cases the virtualenv starts but you don't get the (myenv) bahavior. Run the following to see if the virtualenv is activated.
which python

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.

Anaconda and Git Bash in Windows - conda: command not found

I've installed Anaconda and set Path environment variable to C:\Anaconda3; C:\Anaconda3\Scripts.
Then I try to run in Git Bash
conda install python
But there is an error message "bash: conda: command not found". I would like to know why.
To be able to run conda on gitbash you need to add it to the path. Many times I've seen that's done by default - as shown in the setup for this workshop. If it doesn't, as it seems your case, then you can run their setup directly by running:
. /c/Anaconda3/etc/profile.d/conda.sh
After running that you should be able to run conda commands.
To keep this setup permanently you can add such line on your .profile or .bashrc file (read more about their differences). A way of doing so is running the follwing:
echo ". /c/Anaconda3/etc/profile.d/conda.sh" >> ~/.profile
You may encounter problems if the path where Anaconda was installed contains spaces (e.g., C:\Program Files). In that case you would need to change the anaconda location or edit conda.sh script with something like:
sed -e '/^_CONDA_EXE=.*/a alias myconda="${_CONDA_EXE/ /\\\\ }"' \
-e 's/\$_CONDA_EXE/myconda/g' /c/Program\ Files/Anaconda3/etc/profile.d/conda.sh > conda_start.sh
This sed command inserts a new alias definition myconda which changes the anaconda path from Program Files to Program\ Files so bash doesn't stop with an error like this one:
bash: /c/Program: No such file or directory
The second sed command replaces the _CONDA_EXE variable by the new alias created.
Since the above doesn't modify the file provided by anaconda, you will need to update your .profile file to load the file we've just created, conda_start.sh, instead.
First, you need to move to the directory where conda is located.
(some path such as C/Anaconda3/Scripts or ../miniconda3/Scripts or anaconda3/bin)
then, open the terminal.
(or, if you use Windows and can't find where the conda is, try moving to directory such as C:\Users\User_Name\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Anaconda3 (64-bit) and open the Anaconda prompt.)
Then, do this:
conda init
or something like
conda init bash
if you use Mac OS:
conda init zsh
will work well.
if you wanna use different shell,
conda init [shell_name]
[shell_name] can be: bash, cmd.exe, fish, powershell, tcsh, xonsh, zsh, etc.
use conda init --help for more info.
Joining #dvdgc13. In my case, I fixed the problem by adding
. C:/Users/user/Anaconda3/etc/profile.d/conda.sh
to my .bash_profile.
I tried to do the same thing as you did but I couldn't get it to work. starriet had the working answer but I am going to make it easier for everyone else reading. You can directly open command windows with explorer instead of struggling with paths.
Find your Anaconda3 folder with Windows Explorer This could be a user install where would be in your user folder such as "C:/Users/your_name/Anaconda3".
Shift + Right Click on the explorer and click on "Open PowerShell Windows Here". Note: you can just click "Git Bash" and you open Bash instead which makes no difference to the command.
Type in "conda init" in the PowerShell window. This works even if you don't have the right paths because the command line looks for the right exe in the current dictionary. If you scroll down in the explorer, you should be able to find it.
My PowerShell window would look a little different than yours because of my prompt but it makes no difference.
Exit the PowerShell and open Git Bash. Type "conda" to confirm that things work.
For MAC users, do this:
$ echo ". /usr/local/anaconda3/etc/profile.d/conda.sh" >> ~/.bashrc
$ source ~/.bashrc
I tried many ways, but they are incomplete until I run the following commands:
Go to the path of anaconda3 is C:\Users\USER_NAME\anaconda3 and open commend line over there and print the following: (YOUR_PATH = C:\Users\USER_NAME\anaconda3 )
echo 'export PATH="$PATH:[YOUR_PATH]:[YOUR_PATH]/Scripts"' >> .bashrc
echo 'alias python="winpty python.exe"' >> .bashrc
If Git bash is opened, close it and reopen it again and type the following to make sure anaconda and python work without problems:
conda --version
python -- version
If you see the versions are printed, everything works well.
In my case conda command was recognised in cmd, but not in bash. I used conda init bash instead of simple conda init in cmd.
This command modified the .bash_profile file, which was residing in my C:\Users\xyz directory, and added the following code in it
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
eval "$('/C/Users/Saifullah/miniconda3/Scripts/conda.exe' 'shell.bash' 'hook')"
# <<< conda initialize <<<
now it is working in bash terminal too.

Categories