Multiple python virtual env - python

Suppose i have normal system python 2.7 packages in system locations
Then i do
virtualenv env1
I install all requirements there
Then i deactivate that and do
export PYTHONPATH=$PYTHONPATH:/path/to/env1
Then i do virtualenv env2 and then install new requirements there
then i do this again
export PYTHONPATH=$PYTHONPATH:/path/to/env2
So my questions is
All the packagaes which are installed in env1 , will not be installed in env2 or env2 will install new packagaes.
I ahve problem where if i pip install packagae1 while env2 is activated. it says requirement already met
Then i open python shell and do import mymodule. It says module not found. I can see that module was there in env1.
I had to remove that module from env1 and then install on env2 and then it work.
I want to know whay is that

Have you activated env2 before installing module ?
source bin/activate
If you want to uninstall any module from virtualenv, then use
pip uninstall module_name

Firstly, do not change PYTHONPATH manually.
Steps should look something like this:
[root#demo src]$ source /usr/local/env1/bin/activate
(env1)[root#demo src]$ # pip install blah
(env1)[root#demo src]$ source /usr/local/env2/bin/activate
(env2)[root#demo src]$ #pip install blah
(env2)[root#demo src]$

Probably you have not activated the virtual environment (lets call it as venv) and installed the package system wide.
I suggest you to try activating the venv first and then proceed with installations in either of the venv.
You can activate venv using following code :
cd ~/venv/
source /bin/activate

Related

ML and Django: using Conda and Pip depending on what I am doing... no? [duplicate]

conda 4.2.13
MacOSX 10.12.1
I am trying to install packages from pip to a fresh environment (virtual) created using anaconda. In the Anaconda docs it says this is perfectly fine. It is done the same way as for virtualenv.
Activate the environment where you want to put the program, then pip install a program...
I created an empty environment in Ananconda like this:
conda create -n shrink_venv
Activate it:
source activate shrink_venv
I then can see in the terminal that I am working in my env (shrink_venv). Problem is coming up, when I try to install a package using pip:
(shrink_venv): pip install Pillow
Requirement already satisfied (use --upgrade to upgrade): Pillow in /Library/Python/2.7/site-packages
So I can see it thinks the requirement is satisfied from the system-wide package. So it seems the environment is not working correctly, definitely not like it said in the docs. Am I doing something wrong here?
Just a note, I know you can use conda install for the packages, but I have had an issue with Pillow from anaconda, so I wanted to get it from pip, and since the docs say that is fine.
Output of which -a pip:
/usr/local/bin/pip
/Users/my_user/anaconda/bin/pip
** UPDATE **
I see this is pretty common issue. What I have found is that the conda env doesn't play well with the PYTHONPATH. The system seems to always look in the PYTHONPATH locations even when you're using a conda environment. Now, I always run unset PYTHONPATH when using a conda environment, and it works much better. I'm on a mac.
For others who run into this situation, I found this to be the most straightforward solution:
Run conda create -n venv_name and conda activate venv_name, where venv_name is the name of your virtual environment.
Run conda install pip. This will install pip to your venv directory.
Find your anaconda directory, and find the actual venv folder. It should be somewhere like /anaconda/envs/venv_name/.
Install new packages by doing /anaconda/envs/venv_name/bin/pip install package_name.
This should now successfully install packages using that virtual environment's pip!
All you have to do is open Anaconda Prompt and type
pip install package-name
It will automatically install to the anaconda environment without having to use
conda install package-name
Since some of the conda packages may lack support overtime it is required to install using pip and this is one way to do it
If you have pip installed in anaconda you can run the following in jupyter notebook or in your python shell that is linked to anaconda
pip.main(['install', 'package-name'])
Check your version of pip with pip.__version__. If it is version 10.x.x or above, then install your python package with this line of code
subprocess.check_call([sys.executable, '-m', 'pip', 'install', '--upgrade', 'package-name'])
In your jupyter notebook, you can install python packages through pip in a cell this way;
!pip install package-name
or you could use your python version associated with anaconda
!python3.6 -m pip install package-name
I solved this problem the following way:
If you have a non-conda pip as your default pip but conda python is your default python (as below)
>which -a pip
/home/<user>/.local/bin/pip
/home/<user>/.conda/envs/newenv/bin/pip
/usr/bin/pip
>which -a python
/home/<user>/.conda/envs/newenv/bin/python
/usr/bin/python
Then instead of just calling
pip install <package>, you can use the module flag -m with python so that it uses the anaconda python for the installation
python -m pip install <package>
This installs the package to the anaconda library directory rather than to the library directory associated with (the non-anaconda) pip
EDIT:
The reason this works is as follows:
the command pip references a specific pip file/shortcut (which -a pip tells you which one). Similarly, the command python references a specific python file (which -a python tells you which one). For one reason or another these two commands can become unsynchronized, so that your 'default' pip is in a different folder than your default python, and therefore is associated with a different version of python.
In contrast, the python -m pip construction does not use the shortcut that the pip command points to. Instead, it asks python to find its version of pip and use that version to install a package.
This is what worked for me (Refer to image linked)
Open Anaconda
Select Environments in the left hand pane below home
Just to the right of where you selected and below the "search environments" bar, you should see base(root). Click on it
A triangle pointing right should appear, click on it an select "open terminal"
Use the regular pip install command here. There is no need to point to an environment/ path
For future reference, you can find the folder your packages are downloading to if you happen to have a requirement already satisfied. You can see it if you scroll up in the terminal. It should read something like: requirement already satisfied and then the path
[]
If you didn't add pip when creating conda environment
conda create -n env_name pip
and also didn't install pip inside the environment
source activate env_name
conda install pip
then the only pip you got is the system pip, which will install packages globally.
Bus as you can see in this issue, even if you did either of the procedure mentioned above, the behavior of pip inside conda environment is still kind of undefined.
To ensure using the pip installed inside conda environment without having to type the lengthy /home/username/anaconda/envs/env_name/bin/pip, I wrote a shell function:
# Using pip to install packages inside conda environments.
cpip() {
ERROR_MSG="Not in a conda environment."
ERROR_MSG="$ERROR_MSG\nUse \`source activate ENV\`"
ERROR_MSG="$ERROR_MSG to enter a conda environment."
[ -z "$CONDA_DEFAULT_ENV" ] && echo "$ERROR_MSG" && return 1
ERROR_MSG='Pip not installed in current conda environment.'
ERROR_MSG="$ERROR_MSG\nUse \`conda install pip\`"
ERROR_MSG="$ERROR_MSG to install pip in current conda environment."
[ -e "$CONDA_PREFIX/bin/pip" ] || (echo "$ERROR_MSG" && return 2)
PIP="$CONDA_PREFIX/bin/pip"
"$PIP" "$#"
}
Hope this is helpful to you.
python -m pip install Pillow
Will use pip of current Python activated with
source activate shrink_venv
For those wishing to install a small number of packages in conda with pip then using,
sudo $(which pip) install <instert_package_name>
worked for me.
Explainaton
It seems, for me anyway, that which pip is very reliable for finding the conda env pip path to where you are. However, when using sudo, this seems to redirect paths or otherwise break this.
Using the $(which pip) executes this independently of the sudo or any of the commands and is akin to running /home/<username>/(mini)conda(3)/envs/<env_name>/pip in Linux. This is because $() is run separately and the text output added to the outer command.
All above answers are mainly based on use of virtualenv. I just have fresh installation of anaconda3 and don't have any virtualenv installed in it. So, I have found a better alternative to it without wondering about creating virtualenv.
If you have many pip and python version installed in linux, then first run below command to list all installed pip paths.
whereis pip
You will get something like this as output.
pip: /usr/bin/pip /home/prabhakar/anaconda3/bin/pip /usr/share/man/man1/pip.1.gz
Copy the path of pip which you want to use to install your package and paste it after sudo replacing /home/prabhakar/anaconda3/bin/pip in below command.
sudo /home/prabhakar/anaconda3/bin/pip install <package-name>
This worked pretty well for me. If you have any problem installing, please comment.
if you're using windows OS open Anaconda Prompt and type activate yourenvname
And if you're using mac or Linux OS open Terminal and type source activate yourenvname
yourenvname here is your desired environment in which you want to install pip package
after typing above command you must see that your environment name is changed from base to your typed environment yourenvname in console output (which means you're now in your desired environment context)
Then all you need to do is normal pip install command e.g pip install yourpackage
By doing so, the pip package will be installed in your Conda environment
I see a lot of good answers here but still wanted to share mine that worked for me especially if you are switching from pip-era to conda-era. By following this, you can install any packages using both conda and pip.
Background
PIP - Python package manager only
Conda - Both package and environment manager for many languages including Python
Install Pip by default every time you create a new conda environment
# this installs pip for your newly created environment
conda create -n my_new_env pip
# activate your new conda environment
conda activate my_new_env
# now you can install any packages using both conda and pip
conda install package_name
#or
pip install package_name
This gives you the flexibility to install any packages in conda environment even if they are not available in conda (e.g. wordcloud)
conda activate my_new_env
# will not work as wordcloud is not available in conda
conda install wordcloud
# works fine
pip install wordcloud
I was facing a problem in installing a non conda package on anaconda, I followed the most liked answer here and it didn't go well (maybe because my anaconda is in F directory and env created was in C and bin folder was not created, I have no idea but it didn't work).
According to anaconda pip is already installed ( which is found using the command "conda list" on anaconda prompt), but pip packages were not getting installed so here is what I did, I installed pip again and then pip installed the package.
conda install pip
pip install see
see is a non-conda package.
Depends on how did you configure your PATH environmental variable.
When your shell resolves the call to pip, which is the first bin it will find?
(test)$ whereis pip
pip: /home/borja/anaconda3/envs/test/bin/pip /home/borja/anaconda3/bin/pip
Make sure the bin folder from your anaconda installation is before /usr/lib (depending on how you did install pip). So an example:
(test) borja#xxxx:~$ pip install djangorestframework
....
Successfully installed asgiref-3.2.3 django-3.0.3 djangorestframework-3.11.0 pytz-2019.3 sqlparse-0.3.1
(test) borja#xxxx:~$ conda list | grep django
django 3.0.3 pypi_0 pypi
djangorestframework 3.11.0 pypi_0 pypi
We can see the djangorestframework was installed in my test environment but if I check my base:
(base) borja#xxxx:~$ conda list | grep django
It is empty.
Personally I like to handle all my PATH configuration using .pam_environment, here an example:
(base) borja#xxxx:~$ cat .pam_environment
PATH DEFAULT=/home/#{PAM_USER}/anaconda3/bin:${PATH}
One extra commet. The way how you install pip might create issues:
You should use: conda install pip --> new packages installed with pip will be added to conda list.
You shodul NOT use: sudo apt install python3-pip --> new packages will not be added to conda list (so are not managed by conda) but you will still be able to use them (chance of conflict).
Well I tried all the above methods. None worked for me because of an issue with the proxy settings within the corporate environment. Luckily I could open the pypi website from the browser. In the end, the following worked for me:
Activate your environment
Download the .whl package manually from
https://pypi.org/simple/<package_name>/
Navigate to the folder where you have downloaded the .whl from the command line with your environment activated
perform:
pip install package_name_whatever.whl
If you ONLY want to have a conda installation. Just remove all of the other python paths from your PATH variable.
Leaving only:
C:\ProgramData\Anaconda3
C:\ProgramData\Anaconda3\Scripts
C:\ProgramData\Anaconda3\Library\bin
This allows you to just use pip install * and it will install straight into your conda installation.
I know the original question was about conda under MacOS. But I would like to share the experience I've had on Ubuntu 20.04.
In my case, the issue was due to an alias defined in ~/.bashrc: alias pip='/usr/bin/pip3'. That alias was taking precedence on everything else.
So for testing purposes I've removed the alias running unalias pip command. Then the corresponding pip of the active conda environment has been executed properly.
The same issue was applicable to python command.
Given the information described in this Anaconda blog post, I think the best practice would be to create an environment file so that your conda environments can be created predictably.
I tried a few of the answers posted here without success and I didn't feel like messing around with python paths etc. Instead, I added an environment.yml file similar to this:
name: your-environment-name
channels:
- defaults
dependencies:
- python=3.9.12
- requests=2.28.1
- pandas=1.4.4
- pip=21.2.4
- pip:
- python-dotenv==0.19.2
This guarantees that you install all conda dependencies first, then install pip in the conda environment and use it to install dependencies that are unavailable through conda. This is predictable, reusable, and follows the advice described in the blog post.
You then create a new conda environment using the file with this command:
conda env create -f environment.yml
Uninstall the duplicated python installation. Just keep anaconda and create an env with the desired python version as specified here: https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-python.html. Then your python and pip versions will change as you switch between envs.
I've looked at this answer and many other answers for hours today and couldn't figure this out with 30 years programming experience.
I ran:
conda create -n myenv python=3.9
conda activate myenv
and could not use pip. However, in another environment such as myenv2, myenv3, myenv4 it worked.
I was obtaining the dreaded urllib3 httpsconnection error.
So thought it has to be a missing urllib3 error or something else. It turns out that it was much more sinister than that. Unfortunately it works in other environments and for me I thought that it was related to the fact I'm using Debian on Windows 10 with WSL2. The fix was simple:
rm -rf $HOME/.cache
The pip cache was mangled from a previous install of the same environment. Probably due to the fact I had run an update on conda base and done a distribution upgrade. Because I'm wanting to run a production system with apache2 using a WSGI environment with flask, I want to always have the same conda instance name. So this was a must fix!

Pip installs packages in the wrong directory with virtualenv

With Python 3.7 on OS X
I set up a virtual environment then
$ source venv/bin/activate
$ pip install numpy
$ which pip
pip is /Users/me/PycharmProjects/Test1/venv/bin/pip
(venv)
But rather than installing in the virtual environment numpy is installed in
/usr/local/lib/python2.7
and numpy doesn't appear in pip list
The issue occurs with both Python installed via the Python download or via brew.
What possible settings could be causing the package to be installed in the wrong location.
To answer my own question.
There was an invisible
~/.config/pip/pip.conf
file. That contained these lines:
[global]
target = /usr/local/lib/python2.7/site-packages
This file was a few years old, so I'm unsure how it got there but removing it resolved the issue.
What worked for me :
I just created a pip.ini file by myself on my venv root folder .
filled that file like this :
[global]
target=D:\Dropbox\online store\django\ve\lib\site-packages
3)after restarting venv, by using this command
python -m pip install <package name>
now I am able to install packages on my venv ( instead of being installed globally )
I had this issue because I renamed the root directory of my project. Looking at the venv/bin/activate script, I could see references to the old name.
Could probably fix it by hand, but I just deleted the venv folder and re-created it.

No module named win32com

I've just installed Python for the first time and I'm trying to reference the win32com module however, whenever I try to import it I get the message "no module name win32com".
Any ideas?
the below plus add pywin32 in PyCharm setting work for me
python -m pip install pywin32
As it is not built into Python, you will need to install it.
pip install pywin
Since win32com is a Windows-specific package, this answer will be geared towards Windows users.
Option 1: Install locally with pipenv (recommended)
You can use a package manager like pipenv to manage your dependencies.
Ensure you have pipenv installed (pip install pipenv).
In your project directory, run pipenv install pypiwin32 to install the package.
Now you can run your code using commands like the following pipenv run main.py
Example main.py code:
import win32com
print(win32com)
Option 2: Install locally with venv (recommended)
If pipenv isn't your thing, you can use the built-in virtual environments.
From your project directory, run python -m venv venv to setup you virtual environment.
Run venv\Scripts\activate.bat from your project directory whenever you want to use this virtual environment (you will see (venv) added to your shell prompt to know it's activated).
Run pip install pypiwin32 from your active virtual environment to install the package.
You can run your code like python main.py so long as the virtual environment is active.
Option 3: Install globally (typically not recommended)
This is not typically recommended, but included anyway.
Using pip install pypiwin32 you can install the package globally.
Then you can run your code with just python main.py.
This will work as well
python -m pip install pywin32
You should try using pip this way:
pip install pypiwin32
It is pypiwin32 which should work.
When working with python projects its always a good idea to create a so called virtual environment, this way your modules will be more organized and reduces the import errors.
for example lets assume that you have a script.py which imports multiple modules including pypiwin32.
here are the steps to solve your problem:
1. depending on you operating system you need to download and install virtualenv package, in debian its as simple as sudo apt install virtualenv .
2. after installing 'virtualenv' package go to your project/script folder and create a virtualenv folder with virtualenv venv it creates a folder named venv in that directory.
3. activate your virtualenv source /path/to/venv/bin/activate if your already in the directory where venv exists just issue source venv/bin/activate
4. after activating your venv install you project dependencies pip install pypiwin32 or pip install pywin
5. run your script, it wont throw that error again :)

virtualenv does not include pip

I am trying to create a virtual environment using virtualenv on Mac OS X El Capitan. I have installed Python 2.7.11 with brew, which includes pip, wheel and setuptools by default.
Hovewer, when I try to install virtualenv following instructions in the documentation or from any other resource, I get several problems:
virtualenv executable is not placed in /usr/local/bin after pip makes its job, so I need to ln -s it by hand (it may indicate, that there is something wrong with installation on this step).
After I run virtualenv venv, it creates new environment, catches Python 2.7.11 from brew-installation, but: there is no pip inside bin folder. That means, that if I try which pip, having venv activated, it returns a global position of pip — /usr/local/bin/pip, not /path/to/venv/bin/pip.
As a consequence, installing packages inside venv uses global pip and installs them to a global sites-packages, not that inside venv, and it's quite the opposite of what environment should do.
Could you please suggest what may be wrong and how to fix it?
EDIT: The thing to mention is that I used to have other versions of Python installed on my computer, which I have recently deleted as it is described in this answer. Maybe it causes the issue, and some more thorough cleaning is needed.
Try removing or renaming the .pydistutils.cfg file in your home directory, e.g. by renaming with mv ~/.pydistutils.cfg ~/oldpydistutils.cfg
I'm putting a detailed answer here to help others, but the original credit goes to this answer. If you know what specifically in .pydistutils.cfg was causing the problem, let me know!
I was having the same issue: my virtual environments were created without a local copy of pip, although they had a local copy of python. This meant that using $ pip from within the virtual environment installed to the global package location, and was not visible to the environment's python.
How I diagnosed this on my machine:
I create a virtualenvironment with $ virtualenv env
Activated the virtual environment with $ source env/bin/activate
Checked python location: run (env)$ which python with output /Users/<username>/env/bin/python (as expected)
Checked pip location: run (env)$ which pip with output /usr/local/bin/pip (NOT expected)
To check where our packages are going, we can try to install a package in the virtual environment:
Try to install a package: (env)$ pip install HTTPServer which succeeds
Try to run the package: (env)$ python -m HTTPServer which fails with error /Users/emunsing/env/bin/python: No module named HTTPServer
To double-check, try to install again: (env)$ pip install HTTPServer which produces Requirement already satisfied (use --upgrade to upgrade): HTTPServer in /usr/local/lib/python2.7/site-packages
Double-checking, we see that there's no Pip in the environment's /bin folder:
$ ls env/bin
activate activate.fish python python2
activate.csh activate_this.py python-config python2.7
And so while the system finds the local python version, it can't find a local pip to use and traverses the $PATH. It ended up using pip from /usr/local/bin, leaving me unable to install packages locally to the virtual environment.
Here's what I tried:
- Reinstalling python brew uninstall python followed by brew upgrade and brew install python --build-from-source
- Installing pip using the get-pip.py command as described in the Pip documentation
Here's what I ruled out:
- I was not using sudo pip ... which caused similar problems in this other question and haven't done so at any time on this Python/pip install
- My virtual environment didn't show a local installation of pip, as was the case in these similar questions: This one for Windows, This one for Mac OS X.
Ultimately, I found that eliminating the ~/.pydistutils.cfg file fixed the problem, allowing for fresh virtual environments that had their own local pip. The contents of my ~/.pydistutils.cfg file were:
[global]
verbose=1
[install]
install-scripts=$HOME/bin
[easy_install]
install-scripts=$HOME/bin
Simply renaming the ~/.pydistutils.cfg file appears to fix the problem: it seems that although this file was created by the homebrew installation, some settings in this file may be incompatible with virtualenv. While removing this file hasn't had any bad effects on my system, you may need to use the --user flag when installing packages with pip to the global environment (e.g. $ pip install --user HTTPServer). Here are more details on .pydistutils.cfg if you want to work on tailoring it for your needs.
virtualenv executable is not placed in /usr/local/bin after pip makes its job, so I need to ln -s it by hand (it may indicate, that there is something wrong with installation on this step).
Don't do that. That will only hide the bug and not solve the problem. Here's a short guide how to debug this kind of issues:
Start with which -a python. The first path you see should be /usr/local/bin/python, if not check your PATH variable.
Next, check which -a pip. Again the first path should be /usr/local/bin/pip. If not, run python -m ensurepip and recheck.
Now install virtualenv using pip install virtualenv, after that check the output of which -a virtualenv. The first path should be /usr/local/bin/virtualenv, if not check the output of env |grep PYTHON for unexpected environment variables.
Finally check the output of virtualenv --version to make sure you have the latest version.
I had the issue when running virtualenv: "ImportError: No module named pip."
My solution was to downgrade virtualenv. I had 16.2.0.
pip uninstall virtualenv
pip install virtualenv==15.1.0
Just hit same issue on Linux. Seems like there are multiple causes of this issue, but for me answer was to remove ~/.pip/.
Details: I had this in my .pip/pip.conf for some reason I can't remember:
$ cat ~/.pip/pip.conf
[global]
use_https = True
index = https://pypi.python.org/pypi
prefix = /home/sam/local/
and was using local versions on Python, Pip installed at ~/local/. For some reason virtualenv installed pip must pick up prefix = /home/sam/local/ setting and pip was being installed there.
Try this: sudo apt install pythonV.v-distutils.
In my case V.v == 3.8.
This worked for me.

Virtualenv Command Not Found

I couldn't get virtualenv to work despite various attempts. I installed virtualenv on MAC OS X using:
pip install virtualenv
and have also added the PATH into my .bash_profile. Every time I try to run the virtualenv command, it returns:
-bash: virtualenv: command not found
Every time I run pip install virtualenv, it returns:
Requirement already satisfied (use --upgrade to upgrade): virtualenv in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
I understand that in mac, the virtualenv should be correctly installed in
/usr/local/bin
The virtualenv is indeed installed in /usr/local/bin, but whenever I try to run the virtualenv command, the command is not found. I've also tried to run the virtualenv command in the directory /usr/local/bin, and it gives me the same result:
-bash: virtualenv: command not found
These are the PATHs I added to my .bash_profile
export PATH=$PATH:/usr/local/bin
export PATH=$PATH:/usr/local/bin/python
export PATH=$PATH:/Library/Framework/Python.framework/Version/2.7/lib/site-packages
Any workarounds for this? Why is this the case?
If you installed it with
pip install virtualenv
You need to run
sudo /usr/bin/easy_install virtualenv
which puts it in /usr/local/bin/.
The above directory by default should be in your PATH; otherwise, edit your .zshrc (or .bashrc) accordingly.
I faced the same issue and this is how I solved it:
The issue occurred to me because I installed virtualenv via pip as a regular user (not root). pip installed the packages into the directory ~/.local/lib/pythonX.X/site-packages
When I ran pip as root or with admin privileges (sudo), it installed packages in /usr/lib/pythonX.X/dist-packages. This path might be different for you.
virtualenv command gets recognized only in the second scenario
So, to solve the issue, do pip uninstall virtualenv and then reinstall it with sudo pip install virtualenv (or install as root)
The simplest answer. Just:
pip uninstall virtualenv
and then:
pip install virtualenv
Or you maybe installed virtualenv with sudo, in that case:
pip install --user virtualenv
python3 -m virtualenv virtualenv_name
or
python -m virtualenv virtualenv_name
On Ubuntu 18.04 LTS I also faced same error.
Following command worked:
sudo apt-get install python-virtualenv
I had the same issue. I used the following steps to make it work
sudo pip uninstall virtualenv
sudo -H pip install virtualenv
That is it. It started working.
Usage of sudo -H----> sudo -H: set HOME variable to target user's home dir.
I had same problem on Mac OS X El Capitan.
When I installed virtualenv like that sudo pip3 install virtualenv I didn't have virtualenv under my command line.
I solved this problem by following those steps:
Uninstall previous installations.
Switch to super user account prior to virtualenv installation by calling sudo su
Install virtualenv by calling pip3 install virtualenv
Finally you should be able to access virtualenv from both user and super user account.
Figure out the problem
Try installing with the --verbose flag
pip install virtualenv --verbose
Output will look something like this
..
Using cached virtualenv-15.1.0-py2.py3-none-any.whl
Downloading from URL https://pypi.python.org/packages/6f/86/3dc328ee7b1a6419ebfac7896d882fba83c48e3561d22ddddf38294d3e83/virtualenv-15.1.0-py2.py3-none-any.whl#md5=aa7e5b86cc8cdb99794c4b99e8d670f3 (from https://pypi.python.org/simple/virtualenv/)
Installing collected packages: virtualenv
changing mode of /home/manos/.local/bin/virtualenv to 755
Successfully installed virtualenv-15.1.0
Cleaning up...
From the output we can see that it's installed at /home/manos/.local/bin/virtualenv so let's ensure PATH includes that.
echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
In my case we can clearly see that /home/manos/.local/bin is totally missing and that's why the shell can't find the program.
Solutions
We can solve this in many ways:
We can install directly to a specific directory by fiddling with pip options (not recomended).
Create appropriate symlinks at /usr/local/bin or similar.
Append /home/manos/.local/bin to PATH.
Install as sudo to install directly to /usr/local/bin
The two last options are probably the most sensible. The last solution is the simplest so therefore I will just show solution 3.
Add this to ~/.profile:
PATH="$PATH:$HOME/.local/bin"
Logout out and in again and it should work.
Found this solution and this worked perfectly for me.
sudo -H pip install virtualenv
The -H sets it to the HOME directory, which seems to be the issue for most people.
Personally. I did the same steps you did on a fresh Ubuntu 20 installation (except that I used pip3). I got the same problem, and I remember I solved it this way:
python3 -m virtualenv venv
Link to understand the -m <module-name> notation.
You are having this error :
zsh: command not found: virtualenv
Because most probably you tried to install virtualenv without typing sudo beforehand.
If you try to add it to /usr/local/bin, this may result on syntax errors as the packages are not properly isntalled/copied:
SyntaxError: invalid syntax
File "build/bdist.macosx-12.0-x86_64/egg/platformdirs/__main__.py", line 16
def main() -> None:
^
In case you have tried to install virtualenv via pip without sudo rights, you need first to uninstall it:
pip3 uninstall virtualenv
Then install it using sudo:
sudo pip3 install virtualenv
Next you just need to activate the env:
virtualenv env
source env/bin/activate
In my case, I ran pip show virtualenv to get the information about virtualenv package. I will look similar to this and will also show location of the package:
user#machine:~$ pip show virtualenv
Name: virtualenv
Version: 16.2.0
Summary: Virtual Python Environment builder
Home-page: https://virtualenv.pypa.io/
Author: Ian Bicking
Author-email: ianb#colorstudy.com
License: MIT
Location: /home/user/.local/lib/python3.6/site-packages
Requires: setuptools
From that grab the part of location up to the .local part, which in this case is /home/user/.local/. You can find virtualenv command under /home/user/.local/bin/virtualenv.
You can then run commands like /home/user/.local/bin/virtualenv newvirtualenv.
You said that every time you run the pip install you get Requirement already satisfied (use --upgrade to upgrade): virtualenv in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages. What you need to do is the following:
Change Directory (go to to the one where the virtualenv.py)
cd /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
If you do an ls you will see that the script is there virtualenv.py
Run the script like this:
python virtualenv.py --distribute /the/path/at/which/you/want/the/new/venv/at theNameOfTheNewVirtualEnv
Hope this helps. My advice would be to research venvs more. Here is a good resource: https://www.dabapps.com/blog/introduction-to-pip-and-virtualenv-python/
I had troubles because I used apt to install python-virtualenv package.
To get it working I had to remove this package with apt-get remove python-virtualenv and install it with pip install virtualenv.
Ensure that virtualenv is executable.
If virtualenv is not found, running the full path (/usr/local/bin/virtualenv) should work.
I had the same problem for a long time.
I solved it by running these two commands, first is to install second is to activate the env:
python3 -m pip install virtualenv
python3 -m virtualenv yourenvname
Note that I'm using python3, you can change it to just python if python3 fails.
Thanks.
I think your problem can be solved using a simple symbolic link, but you are creating the symbolic link to the wrong file. As far as I know virtualenv is installed to /Library/Frameworks/Python.framework/Versions/2.7/bin/virtualenv, (you can change the numbers for your Python version) so the command for creating the symbolic link should be:
ln -s /Library/Frameworks/Python.framework/Versions/2.7/bin/virtualenv /usr/local/bin/virtualenv
On ubuntu 18.4 on AWS installation with pip don't work correctly.
Using apt-get install the problem was solved for me.
sudo apt-get install python-virtualenv
and to check
virtualenv --version
Same problem:
So I just did pip uninstall virtualenv
Then pip install virtualenv
pip install virtualenv --user
Collecting virtualenv
Using cached https://files.pythonhosted.org/packages/b6/30/96a02b2287098b23b875bc8c2f58071c35d2efe84f747b64d523721dc2b5/virtualenv-16.0.0-py2.py3-none-any.whl
Installing collected packages: virtualenv
Then I got this :
The script virtualenv is installed in '/Users/brahim/Library/Python/2.7/bin' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
which clearly says where it is installed and what to do to get it
If you're using Linux, open your terminal and type virtualenv halfway and autocomplete with tab key. If there's no auto-completion install virtualenv on your system by running:
mycomp$sudo apt-get install virtualenv
//if you're already super user.
mycomp#apt-get install virtualenv
You can now navigate to where you want to create your project and do:
myprj$pip3 install virtualenv
//to install python 3.5 and above
myprj$virtualenv venv --python=python3.5
//to activate virtualenv
(venv)myprj$source venv/bin/activate
(venv)myprj$deactivate
This works on Ubuntu 18 and above (not tested in previous versions):
sudo apt install python3-virtualenv
Make sure that you are using
sudo
In this case, at first you need to uninstall the pipenv and then install again using sudo command.
pip uninstall pipenv
sudo pip install pipenv
Follow these basic steps to setup the virtual env
sudo pip install virtualenv virtualenvwrapper
sudo rm -rf ~/get-pip.py ~/.cache/pip
we need to update our ~/.bashrc
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh
The ~/.bashrc file is simply a shell script that Bash runs whenever you launch a new terminal. You normally use this file to set various configurations. In this case, we are setting an environment variable called WORKON_HOME to point to the directory where our Python virtual environments live. We then load any necessary configurations from virtualenvwrapper .
To update your ~/.bashrc file simply use a standard text editor, nano is likely the easiest to operate.
A more simple solution is to use the cat command and avoid editors entirely:
echo -e "\n# virtualenv and virtualenvwrapper" >> ~/.bashrc
echo "export WORKON_HOME=$HOME/.virtualenvs" >> ~/.bashrc
echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc
After editing our ~/.bashrc file, we need to reload the changes:
source ~/.bashrc
Now that we have installed virtualenv and virtualenvwrapper , the next step is to actually create the Python virtual environment — we do this using the mkvirtualenv command.
mkvirtualenv YOURENV
I'm doing Angela Yu's online iOS course and I was getting same problem plus also was getting permission denied error 13 when I was trying to run virtualenv --python=/{myPath} {newVirtualEnvName}
I solved it by:
switching to sudo user sudo su
navigating to my destination folder (where I want my new virtual env to live) ie. /Users/muUserName/Environments/
run command python -m virtualenv python27 where python27 is a name of my new virtual environment
above created folder pathon27 in my Environments folder, and then I was able to run source python27/bin/activate to start my virtualenv
After upgrading MacOS Monterey from 12.5.1 to 12.6, I was no longer able to run virtualenv. Since I had brew on my mac, installed like this:
$ brew install virtualenv
...
==> Installing virtualenv
==> Pouring virtualenv--20.16.5.monterey.bottle.tar.gz
/usr/local/Cellar/virtualenv/20.16.5: 949 files, 20.3MB
==> Running `brew cleanup virtualenv`...
...
Of course, brew decided to upgrade various other packages I had as well, but virtualenv was available again thereafter.
For me it was installed in this path (python 2.7 on MacOS):
$HOME/Library/Python/2.7/bin
Simple answer is that if you are not a sudo user as I was not one.You need to add path of your bin folder (/home/myusername/.local/bin).So basically the command line searches in which of these path is the command which you have typed.
export PATH=/home/b18150/.local/bin:/usr/bin:/bin
here it will search in local/bin first then /usr/bin and then /bin.
on Mac
> pip3 install virtualenv
> python3 -m virtualenv [venv_name_you_want]
Q. virtualenv not found
After installing virtualenv, virtualenv exist on the pip3 list. But When to use the "virtualenv [venv_name]" command, it returns "virtualenv not found".
A. Because virtualenv is installed as a module in python3. Not installed as a command tool like python3 in the "/usr/bin/.." path. So this case we can use "python3 -m virtualenv [venv_name]".
And we can see where it is to retry this command "pip3 install virtualenv". Then zsh or your shell tells us kindly this info.
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: virtualenv in /Users/[your-usr-name-here]/Library/Python/3.8/lib/python/site-packages (20.16.5)
Requirement already satisfied: filelock<4,>=3.4.1 in /Users/[your-usr-name-here]/Library/Python/3.8/lib/python/site-packages (from virtualenv) (3.8.0) ...
If you installed it with
pip install virtualenv
Now to use it you need to type this command:
python -m virtualenv name_of_your_virtualenv
in order to activate it:
.\name_of_your_virtualenv\Scripts\activate
if you face a problem activating your virtualenv, it could be Execution Policy Settings. To fix it, you should try executing this command in your command line: Set-ExecutionPolicy Unrestricted -Scope Process. This would allow PowerShell will run the script.
apt update
apt upgrade
apt install ufw python virtualenv git unzip pv
3 commands and everything working!

Categories