Not able to run virtualenv - python

I installed virtual environment using (pip3 install virtualenv). it was installed but when I check. This comes up.
faculty#Facultys-MacBook-Pro ~ % virtualenv --version
zsh: command not found: virtualenv

I believe you might not have you python libs in your path. which is why it is not found by zsh.
Quick fix
I believe you can go with:
python3 -m virtualenv <name of the virtual env>
This should hopefully work and create your virtualenv in the current folder you are in.
Permanent fix
Some smarter people already went through this issue have a look pip installs packages successfully, but executables not found from command line

You need to make sure the following thing:
virtualenv has been only installed in the folder you used to run the pip commmand. There should be a new directory called venv now. Try opening the terminal inside the venv folder bin and type the command again.

Related

Create fresh python venv while local have some package installed [duplicate]

I am very new at command line usage. I am using python 3.7.2, Bash and VSCode Integrated Terminal. I am trying to create a virtual environment using venv and following python documentation:
https://docs.python.org/3/tutorial/venv.html#creating-virtual-environments
The command to use is this one:
$ python3 -m venv test-env
and I get:
bash: python3: command not found
Later I have found a similar answer in an stackoverflow post:
How to create and activate virtual environment in windows 10 using bash command
And I use the command:
py -m virtualenv test-env
and I get this:
No module named virtualenv
I am very new using the command line so i don´t really know what is going on and how to work it around.
Hi i can see that you are using two different tools to create your environment.
Those are "venv" and "virtualenv".
Venv is a library that already comes with your python installation.
Virtualenv is an external one.
I had the same problem before and the solution is very simple.
I recommend you to stick with venv because it works pretty ok and you don´t need to do extra job installing external libraries.
So for solving your problem the Bash Shell is telling you that the command Python3 has not been found.
So try instead just:
python -m venv test-env
Sometimes Python documentation is not accurate enough and I know when you start using commands, accuracy in the sintax is extremely important.
Try this steps,it'll helped you:
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 (OR, python -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....

Python installation directory

On command $which python3$ , the location says /opt/homebrew/bin/python3 on my Mac. Is this okay for python to be in other directory than /usr/local/ ?
Yes. It will work. I mean if you change the location of installation directory, mac os will recognize it and python3 instruction will work.
Yeah it's absolutely ok. But it's better to create a project wise virtual env so that you don't messed up installing so many third party libraries globally in your system which could break system tools and other projects.
Installing vitualenv:
python3 -m pip install --user virtualenv
Creating a virtual environment.
cd {{your project directory}}
python3 -m venv env
Activate the virtualenv:
source env/bin/activate
Now if you run which python you will see the python is from your newly created virtual env.

pip freeze showing all libraries, not the ones in my virtual env

Pip Freeze shows all the libraries I have on my computer not just the libraries in the virtual environment.
I am trying to create a requirements.txt file for my virtual environment. I'm using an anaconda distribution. I am creating a flask app. I have navigated to my project folder created the virtual environment added flask and then when I make the command pip freeze it clearly shows items that are not in my virtual environment like xlwings, pandas and stuff I use that has nothing to do with flask.
Any way I can create a requirements file from my virtual environment.
I can clearly see my virtual environment is active with (venv) to the left.
Edit: I created a short video showing I get the same list of libraries whether I'm in my virtual environment or not. Also I'm showing the site-packages in my virtual environment and showing that these libraries aren't there I'm specifically pointing out xlwings.
https://youtu.be/xEFZ3dSaqoY
So i'm not sure why it was happening, but I deleted the virtual environment and re-created it (I had a previous requirements.txt that was correct). Then I ran pip freeze again and it all worked. Not sure what happened, but it works for me now.
I had this same problem and it happened because i change the name of my venv folder.
To solve this i used this command in my terminal [yourVenv]\Scripts\python -m pip freeze.
If you don't want to do it like this everytime, try creating a new enviorment like this.
[yourVenv]\Scripts\python -m pip freeze > requirements.txt
python -m venv [yourVenv]
[yourVenv]\Scripts\activate
python -m pip install --upgrade pip
pip install -r requirements.txt
You need to activate your pip environment in the console that you are trying to run pip freeze in. That way it uses the environment's pip and not your global pip.
So in your console, navigate to your virtual environment folder. From there go to the "Scripts" folder. Then enter the word "activate" into the console.
You should then see next to the console cursor the name of your virtual environment. At that point you can use the pip that's inside of your virtual environment and all the normal pip commands will point to it.
In my case i was using vscode IDE so when i created .venv its recommended use worksapce, so i choosed yes thats why its took all from my system' lib.
for this you can delete old on .venv and create new one, this time don't choose plugin's recommendations.
for create veirtual enviroment in window/linx
python -m venv .venv or python3 -m venv .venv
to activate .venv win use (.venv\scripts\activate)
and for linux use (source .venv/bin/activate)
all command like pip freeze, pip list, pip freeze > requirements.txt will work

Error "virtualenv : command not found" but install location is in PYTHONPATH

This has been driving me crazy for the past 2 days.
I installed virtualenv on my Macbook using pip install virtualenv.
But when I try to create a new virtualenv using virtualenv venv, I get the error saying "virtualenv : command not found".
I used pip show virtualenv and the location of the installation is "Location: /usr/local/lib/python2.7/site-packages" but I can't figure out where the executable is. I tried dozens other similar looking posts but those solutions do not work for me.
Any ideas what might be going wrong here?
The only workable approach I could figure out (with help from #Gator_Python was to do python -m virtualenv venv. This creates the virtual environment and works as expected.
I have custom python installed and maybe that's why the default approach doesn't work for me.
On macOS Mojave
First check python is in the path.
python --version
Second check pip is installed.
pip --version
If it is not installed.
brew install pip
Third install virtualenv
sudo -H pip install virtualenv
For Python 3
python3 -m virtualenv venv
As mentioned in the comments, you've got the virtualenv module installed properly in the expected environment since python -m venv allows you to create virtualenv's.
The fact that virtualenv is not a recognized command is a result of the virtualenv.py not being in your system PATH and/or not being executable. The root cause could be outdated distutils or setuptools.
You should attempt to locate the virtualenv.py file, ensure it is executable (chmod +x) and that its location is in your system PATH. On my system, virtualenv.py is in the ../Pythonx.x/Scripts folder, but this may be different for you.
Could it be that you are using Anaconda package manager? If so, then it has it's own virtual environment system which you setup as follows:
conda create --name venv
I had the same issue (although on ubuntu), a simple solution is instead of doing pip install virtualenv, you precede the commend with "sudo".
A little inspection reveals the reason behind this fix:
pip install virtualenv tries to put an executable under /usr/local/bin so that it can be invoked from command line, but it failed because only root has write permission to that directory
an alternative is pip install --user virtualenv, here are some further readings 1,2
Had the same problem on Windows. Command not found and can't find the executable in the directory given by pip show.
Fixed it by adding "C:\Users{My User}\AppData\Roaming\Python\Python39\Scripts" to the PATH environment variable.
Install virtualenv from https://pypi.org/project/virtualenv
python -m pip install --user virtualenv
sudo /usr/bin/easy_install virtualenv
I succeded creating manually a link to location/virtualenv.py in /usr/local/bin, naming it virtualenv and adding +x attribute on file
➜ ~ pip show virtualenv
Name: virtualenv
Version: 16.6.0
Summary: Virtual Python Environment builder
Home-page: https://virtualenv.pypa.io/
Author: Ian Bicking
Author-email: ianb#colorstudy.com
License: MIT
Location: /home/prsadev/.local/lib/python2.7/site-packages
Requires:
~ chmod +x /home/prsadev/.local/lib/python2.7/site-packages/virtualenv.py
~ sudo ln -sf /home/prsadev/.local/lib/python2.7/site-packages/virtualenv.py /usr/local/bin/virtualenv
I tried to have virtualenv at a random location & faced the same issue on a UBUNTU machine, when I tried to run my 'venv'. What solved my issue was :-
$virtualenv -p python3 venv
Also,instead of using $activate try :-
$source activate
If you look at the activate script(or $cat activate), you will find the same in comment.
This solved my similar problem!
You need to look online on how to create a virtual environement with python X.X.X (replace x.x.x with your python version)
mine was python 3.4.3 so bellow is how should i deal with it:
sudo python3 -m venv aramisvenv
I use asdf and had to do a reshim after installing virtualenv. asdf reshim
Fixed due to this response

Multiple python installations and pip, dude, where is my site-packages?

As we all know, Mac OS ships with its own python pre installed.
The recommendation seems to be to leave that alone and use homebrew to install a fresh python into the system.
My issue is that after installing python (and pip) using homebrew, pip is installing packages into the Mac OS site-packages instead of my own. I have confirmed I am running the "homebrew" pip:
$ which pip
/usr/local/bin/pip
But then when I pip install something I can se it is installed at:
/lib/python2.7/site-packages
Pip should be installing at /usr/local/lib/python2.7/site-packages unless i'm miss understanding something.
The surprising thing is that checking with -V yields a surprising result:
pip -V
pip 7.1.0 from /usr/local/lib/python2.7/site-packages (python 2.7)
Running pip list just after running pip install does not show the packages that were supposedly just installed by it but went to the wrong site-packages.
Adding to this, the packages installed on the /lib/python2.7/site-packages are not recognized by my $PYTHONPATH and as such I cannot use them.
To add even more confusion, I decided to use a virtualenv, but I was amazed as even using pip with the virtualenv active kept installing to the /lib/python2.7/site-packages instead of the virtualenv site-packages.
So, somehow I ended up with a homebrew pip, that installs packages outside of the homebrew site-packages and a python interpreter that cant use the packages installed by pip.
How do you recommend I go about finding the root cause and having a smooth python experience? :)
You can easily find you site-packages directory by invoking this command
python -c 'import site; print(site.getsitepackages())'
I think after you activate a virtualenv your python path should point to that environments site-package location--if not it's probably not activated. Only once you activate it will you run pip so it installs in that virtual env's site-packages. if it's not activated, it will go in whatever other site-packages it already knows about:
Step 1: create a virtual env
a la... virtualenv venv
Do this only once!
Step 2: Activate the vitual env
something like source /venv/bin/activate
Needs doing every time you want to use this virtual environment
Step 3: run pip commands, watch them get installed in the virtual env site-packages!
If you do step 3 before step 2 your not actually using the virtual environment you created, so all bets are off--That's probably the reason pip is still installing to the old location.
Now, my overall recommendation is to go further and use pyenv to install specific version of python into your /Users/username/.pyenv folder and abandon both the default OSX and homebrew packages. It's simple and you can control easily the exact version of python to use by simple issuing of command to change versions.
THEN use virtualenv in python2 or pyvenv if in python3 (not to be confused with pyenv) to build vitual environments with their own local site-packages to store pip modules. When you activate a virtualenv, your $PYTHONPATH will switch to the specific location.
The flow would then be:
Use pyenv to pull down and switch to a specific version of python you want to use--overriding homebrew and the OSX version.
Create your vitrualenv. This will create a bin that will link to the pyenv python stack you just specified in the previous step.
Activate the virtual env, and proceed.
Totally control your environment!
For one, you could try updating pip with pip install --upgrade pip command, which might or might not redirect your pip path.
Two, and I should have really started with this one is to set the preferred pip executable path in the .bash_profile or .zshrc if you are using one. The way you do it (on Mac) is by holding Shift+Command+Period to reveal hidden files, going to the User folder and opening the .bash_profile/.zshrc with a text editor. Afterward, add the path/to/bin where the pip that you require is. Like export PATH="User/Username/anaconda3/bin:$PATH" or /usr/local/bin or path/to/venv/bin. Whatever code you write in the end will overwrite the previous one.
Three, if you don't wanna change your default pip, but rather wanna use a different version for that specific case just include the full path of the pip executable like /usr/local/bin/pip list or Users/Username/Desktop/venv/bin/pip install module.

Categories

Resources