Cannot create and activate a virtual environment on Git Bash with Python 2.7, Windows - windows

I am using Git Bash on windows with python 2.7 and I want to work in a virtual environment.
When I type
virtualenv venv
Bash says
bash: virtualenv: command not found
That makes me think virtualenv is not installed, then I try to install virtualenv
pip install virtualenv
But again Bash says
bash: pip: command not found
Then by reading this thread python 2.7: cannot pip on windows "bash: pip: command not found" I find out that it cannot find the pip directory, that maybe is the same reason for which it cannot find virtualenv directory.
So I specify the path of pip and I try again to instal virtualenv
python -m pip install virtualenv
It installs virtualenv but then tells
DEPRECATION: Python 2.7 will reach the end of its life on January 1st,
2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python
2.7.
So I go forward and try to activate my virtualenv by typing
virtualenv venv
and as I expected I get the same error of before
bash: virtualenv: command not found
that I try to solve in the same way I did for pip, by typing
python -m virtualenv venv
and this time bash responds with
$ python -m virtualenv venv New python executable in
C:\Users\Tommaso\DJANGO~1\UDEMYD~1\METAGL~2\venv\Scripts\python.exe
Installing setuptools, pip, wheel... done.
So I guess it created my virtualenv named venv, but in my bash terminal I still get the writing "(master)" at the end of the input line, that I guess it means the virtual environment is not active.
How do I activate it?

Solved!
Here is what I did.
First, by following this SO answer, I found out how to make Git Bash use python 3.7 instead of python 2.7 by default:
I opened the file aliases.sh with Notepad++, located at
C:<path where you installed Git Bash>\etc\profile.d\aliases.sh
Under the paragraph
--show-control-chars: help showing Korean or accented characters
I added these lines indicating where the two versions of python I want to switch are located
alias python2='C:/<installation_path_of_python_2.7>/Python 2.7/python.exe'
alias python='C:/<installation_path_of_python_3.7>/Python 3.7/python.exe'
alias pip='C:/<installation_path_of_python_3.7>/Phyton/Scripts/pip.exe'
alias virtualenv='C:/<installation_path_of_python_3.7>/Phyton/Scripts/virtualenv.exe'
You don't really need the last 2 ones, but it will help you a lot, since it enables Git Bash to call pip, virtualenv and other scripts without writing python -m before the name of the script.
You can check out if you did right by typing
python -i
It should return you the latest python version you specified.
Then if I type
python -m virtualenv venv
It installs the virtual environment in the current directory
To activate that, just type
. venv/scripts/activate
If it works, you should see
(venv)
near to your active input line.
To deactivate it just type
deactivate

I had the same problem before. The solution is to install virtualenv first using pip. Then:
enter virtualenv nameOfTheEnvironment
nameOfTheEnvironment\Scripts\activate
You should see something like:
C:\Users\bamidele\Documents\DjangoProjects>virtualenv venv
created virtual environment CPython3.7.2.final.0-64 in 15334ms
creator CPython3Windows(dest=C:\Users\bamidele\Documents\DjangoProjects\venv, clear=False, global=False)seeder FromAppData(download=False, pip=latest, setuptools=latest, wheel=latest, via=copy, app_data_dir=C:\Users\bamidele\AppData\Local\pypa\virtualenv\seed-app-data\v1.0.1)activators BashActivator,BatchActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator
C:\Users\bamidele\Documents\DjangoProjects>venv\Scripts\activate
(venv) C:\Users\bamidele\Documents\DjangoProjects>```
I hope this solves your problem.

In git bash, cd into the virtual environment(venv) directory of your project and use this command
source ./Scripts/activate
to activate virtual environment(venv)

You can go
cd venv\Scripts
path. Then:
Run the command
activate
This run on my Flask project.

Related

I get ImportError whenever I try to install outside of site-packages

When I try:
which -a pip
I get
bailin#LAPTOP-J4I3G886:~/miniconda3/bin$ which -a pip
/home/bailin/.local/bin/pip
/home/bailin/miniconda3/bin/pip
/usr/bin/pip
/bin/pip
Should I get rid of most of the pips? I'm on windows 10, wsl2 ubuntu miniconda3
most unix system comes with python, meaning it comes with pip too. After installing miniconda, I had another pip, that should've been overwritten but didn't. The fix is go to your bashrc file and add this:
alias pip=/home/usr/miniconda3/bin/pip
replace path with wherever your pip is

Switch from Pyenv to Conda

I've been using pyenv but I want to switch to Conda. I use VScode, managed to create a Conda environment and use the proper version of Python but when installing packages with PIP, they still install on pyenv and not conda, how do I change that? Should I just remove pyenv from my computer or can I just change the path for pip?
Not sure if that'll help but I did an echo $PATH and got this list
/opt/homebrew/Caskroom/miniforge/base/condabin:/Users/marc/.pyenv/shims:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
Marc, it is not clear from your question whether you've configured conda correctly for VS Code, which means
setting the default terminal
ensuring the default terminal is correctly reading ~/.profile args
ensuring you can select your miniforge conda env from list of python interpreters.
More details about ensuring conda is configured properly is in this related stackoverflow post.
Finally, please ensure that you pip install inside the activated conda env? This is obvious.... for sanity sake ensure you can see the env activated at the shell prompt inside your VSCode terminal before doing pip / conda install commands. Running which pip at the prompt should confirm that you are using pip inside the miniforge path.

can't create virtual environment using pyenv on macos

I tried to create virtual environment using pyenv virtualenv 3.8.2 myenv, but it failed, i don't know why, i even changed the python version or the virtual environment name, but it still not working.
Some detail:
I tried this in macos and zsh.
➜ ~ pyenv virtualenv 3.8.2 myenv
Looking in links: /var/folders/_9/l8m14fgs6ts9wx0nl1qbzbkm0000gn/T/tmpe9l25o2_
Requirement already satisfied: setuptools in /Users/fitz/.pyenv/versions/3.8.2/envs/myenv/lib/python3.8/site-packages (41.2.0)
Requirement already satisfied: pip in /Users/fitz/.pyenv/versions/3.8.2/envs/myenv/lib/python3.8/site-packages (19.2.3)
rm: /Users/fitz/.pyenv/shims/shims: is a directory
➜ ~ pyenv versions
system
3.7.3
* 3.8.2 (set by /Users/fitz/.python-version)
3.9.5
I had the same issue. For me it was caused by two empty folders in my shims directory. I deleted the folders and it fixed the issue. So, just running the following would fix your issue.
rm -rf ~/.pyenv/shims/shims
rm -rf ~/.pyenv/shims/versions
Try using 'venv' instead of pyenv or pyvenv or virtualenv. Venv is a library that already comes with your python installation. Virtualenv is an external one.pyenv is similar to venv in that it lets you manage multiple python environments. However with pyenv you can't conveniently rollback library installs to some start state and you will likely need admin privileges at some point to update libraries. So I think it is also best to use venv.
First, make a directory :
mkdir testing
Then, moved to this directory named testing :
cd testing
When you type following command in this directory:
python3 -m venv env
You got error like :
The virtual environment was not created successfully because ensurepip is not
available. On Debian/Ubuntu systems, you need to install the python3-venv
package using the following command.
apt install python3.8-venv
Type the following command but before that keep an eye on the version of python you installed on the machine; in my case its python3.8
sudo apt install python3.8-venv
Now, we can create a virtual environment and store its tools in the "bhandari" folder .
python3 -m venv bhandari
Note: you can named this "bhandari" folder; anyname you like( Standard practice is to name it "env" ...)
Now to activate your virtual environment, from the directory of your folder, type the following command this will activate our virtual environment in the “bhandari” folder
source bhandari/bin/activate
If you have successfully activated your virtual environment, you should see the (bhandari) word indicating that we are working in a virtual environment.
After this, we can install anything that will be isolated from the rest of the system....

Issue with activating virtualenv

I installed python environment by means of commands:
SYS_INSTALL="apt-get install -y"
PIP_INSTALL="pip install"
# Be sure to install setuptools before pip to properly replace easy_install.
$SYS_INSTALL git
$SYS_INSTALL python-dev
$SYS_INSTALL python-setuptools
$SYS_INSTALL python-pip
$PIP_INSTALL virtualenv
also was able to create new virtual environment:
virtualenv .env
However, after running a command like:
. .env/bin/activate
I got
-bash: .env/bin/activate: No such file or directory
After reviewing folder .env/bin I found only one python file. Whole list of files here:
.env/lib:
python2.7
.env/include:
python2.7
.env/bin:
python
What is the issue here?
SOLUTION add --always-copy
virtualenv .env --always-copy
For me it works when I do these steps:
Go to the directory/folder that you want
run virtualenv .env
then run source .env/bin/activate
The accepted answer is incomplete! The suggested code left out your error, but didn't comment on it.
The command . .env/bin/activate would indeed do the same as source on the file activate in the folder .env/bin. In fact, apparently the command "source" is an alias for the command ".", and not the other way around. Note that . here has a space after it, and used differently from the . discussed below (which makes files and folders hidden).
What I notice is that you are calling your folder .env, which is not standard practice. Files and folders preceded by . are made "hidden" by Mac OS X. Standard practice is to call a virtual environment directory env or venv, and to call the virtual environment specification file .env.
So, if your spec file is called .env and your virtual environment directory is called env, you can run either
source env/bin/activate
or
. env/bin/activate.
I had the same issue and the following steps resolved it:
$mkdir annotateNLP
$cd annotateNLP
$python -m venv env
$source env/Scripts/activate
Try these commands in the terminal:
$ mkdir djangoapp
$ cd djangoapp
$ python3 -m venv myvenv
$ source myvenv/bin/activate
You can't go straight into activate command without first creating your virtual environment.
you forgot to include source before activating command is
source env/bin/activate
this question is similar to your's
virtualenv is not compatible with this system or executable
where it creates virtualenv but,python file instead of activate in bin
After going to your virtual environment folder .\Scripts\activate.
In my case, I need to install
sudo apt-get install python3-venv
$ virtualenv env
$ cd env/Scripts/
$ . activate
I was facing this same issue. I uninstalled the virtualenv in Ubuntu and then I installed it again. After this nonsense, it works and now I am able to activate my virtualenv through -$source py3/bin/activate.
If installed venv on a Windows machine, run this command (assuming you are in the working directory that has your venv folder):
In bash terminal: source venv/Scripts/activate
In cmd terminal:
venv\Scripts\activate
where venv is the folder name for your virtual environment
For windows using git bash, run the below command:-
source env\Scripts\activate

Using pip version with Python 3.x alongside Python 2.x on Windows

My question is thoroughly based on this question. The big difference is I'm focusing on windows here. The answers/question provided there were for unix distributions.
I've ran Python 2.x for quite a while and have used pip with no problems.
Now I've installed python 3 but when I open the cmd prompt and pip install somemodule it tries to install it for python 2. How can I deal with this situation ?
Both Python's were installed with the Anaconda distribution so I believe both of them come with pip installed. Note: This info come from this question
EDIT:
Found this answer which addresses that issue.
But now when I try to run the command
pip3.5 install pymssql
or
pip-3.5 install pymssql
I get the error pip3.5 is an unknown command.
If I open cmd and type python I receive:
Python 3.5.1 Anaconda 4.0.0
so it shouldn't be a version problem
You will want to make sure you have the correct Anaconda environment activated, which it looks like you have in this case.
conda env list # Display the list of conda environments
In the Windows Command Prompt you should just need to use:
activate py35 # Or whatever your Python 3.5 environment is called. (Mine is root)
pip install pymssql
Instead of pip-3.5.
To install it in another environment (mine is called py27):
activate py27
pip install pymssql
I successfully used this command in both my Python 2.7 and 3.5 Anaconda environments.
To go back to your primary environment (root), just type activate without an environment name after it

Resources