I have successfully installed python 2.7.11 on a shared Bluehost server.
In the home directory I installed get-pip.py When I run that now,
# python get-pip.py
Requirement already up-to-date: pip in ./python/lib/python2.7/site-packages
But when I try to run pip I get,
usernmame#example.com [~]# pip
-bash: pip: command not found
Why is pip not running? How can I check what python packages are installed?
My ~/.bashrc looks like this,
# .bashrc
export PATH=$HOME/python/Python-2.7.11/:$PATH
# User specific aliases and functions
alias mv='mv -i'
alias rm='rm -i'
alias cp='cp -i'
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
Additionally,
# echo $PATH
/usr/local/jdk/bin:/home2/username/python/Python-2.7.11/:/usr/lib64/qt-3.3/bin:/home2/username/perl5/bin:/ramdisk/php/54/bin:/usr/php/54/usr/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11R6/bin:/home2/username/bin
EDIT:
My attempt to add the path to $PATH in ~/.bashrc
export PATH=$HOME/python/Python-2.7.11/:$HOME/python/lib/python2.7/site-packages/:$PATH
But it doesn't work, same error.
Thanks
You can run pip by invoking the -m option when you use python to run pip. Suppose you want to use pip to install a Python package called "package-of-interest" from PyPI. Then, you can install it by running:
python -m pip install package-of-interest
Related
I would like to use virtualenvwrapper with Python 3.6, however, I am working on a Mac which defaults to using Python 2.7 and I am having issues. Here's what I have done so far. Using the following commands, I have found the locations of where each Python version is saved:
>> which python
>> /usr/bin/python
>> which python3
>> /Library/Frameworks/Python.framework/Versions/3.6/bin/python3
I have successfully installed virtualenv and virtualenvwrapper using:
>> pip3 install virtualenv
>> pip3 install virtualenvwrapper
I then search for the location of virtualenv and virtualenv wrapper to confirm their locations:
>> pip3 show virtualenv
>> Location: /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages
>> pip3 show virtualenvwrapper
>> Location: /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages
If I go to this site-packages directory there is the following:
virtualenvwrapper
|--- __pycache__
|--- hook_loader.py
|--- project.py
|--- user_scripts.py
virtualenvwrapper-4.8.2-py2.7-nspkg.pth
virtualenvwrapper-4.8.2.dist-info
According to virtualenvwrapper's documentation, I should add the following to my shell startup file to ensure initialization, changing virtualenvwrapper's path to the one set on my machine:
export WORKON_HOME=~/Envs
$ mkdir -p $WORKON_HOME
$ source /usr/local/bin/virtualenvwrapper.sh
I then searched for where virtualenvwrapper.sh is actually located (for some reason, it is not located where the virtualenvwrapper module is installed):
>> which virtualenvwrapper.sh
>> /Library/Frameworks/Python.framework/Versions/3.6/bin/virtualenvwrapper.sh
I changed my .bashrc file using:
>> nano ~./bashrc
Copied in the following:
# script for virtualenvwrapper
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/Devel
source /Library/Frameworks/Python.framework/Versions/3.6/bin/virtualenvwrapper.sh
I then opened up a new terminal and ran:
>> source `which virtualenvwrapper.sh`
Which gave me:
/usr/bin/python3: No such file or directory
virtualenvwrapper.sh: There was a problem running the initialization hooks.
If Python could not import the module virtualenvwrapper.hook_loader,
check that virtualenvwrapper has been installed for
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3 and that PATH is
set properly.
What I don't understand is why virtualenvwrapper needs to be located at /usr/bin/python3 which is not a directory. Since it is in bin, it is also impossible for me to make it a directory. Additionally, the virtualenvwrapper.hook_loader is not where virtualenvwrapper.sh is which I think may be causing an issue.
I also tried creating a symbolic link to /usr/local/bin because some people stated that it solved their issue but this didn't solve mine:
sudo ln /Library/Frameworks/Python.framework/Versions/3.6/bin/virtualenvwrapper.sh /usr/local/bin/virtualenvwrapper.sh
Another issue altogether is whether or not I need to install it using pip3 if I want to use virtualenvwrapper for Python3. For example, this post states that you only need to pass a flag, however, you should also be wary of installing it on your base Python installation.
Add
export VIRTUALENVWRAPPER_PYTHON=/Library/Frameworks/Python.framework/Versions/3.6/bin/python3
to .bashrc and execute the command in the terminal. That sets the path to Python interpreter for virtualenvwrapper.
I've been keeping all my python2.7 installs in my ~/.local/ directory that way I don't have to sudo every time I want to do a pip install. I also have $HOME/.local/lib/python2.7/site-packages on my $PYTHONPATH. This has worked well for years but now I find myself needing to run python3 programs more frequently. After much research it seems like virtualenv is the most recommended way to deal with python 2 and 3 on the same system. But I am running into troubles. I can spin up a python3 virtual environment but when I try to install new libs with pip, my old global path (i.e. ~/.local/) is still being searched by pip, which makes sense. However, this is even the case if I remove my ~/.local/bin/ directory from my $PATH and unset my $PYTHONPATH.
Here is are the steps I took:
First check the preliminaries before activating virtualenv. (I'm on Ubuntu 16.04 btw)
maddoxw#firefly:~$ echo $PATH
/usr/local/cuda-8.0/bin:/home/maddoxw/.node_modules_global/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/maddoxw/bin:/home/maddoxw/scripts
maddoxw#firefly:~$ echo $PYTHONPATH
maddoxw#firefly:~$ python --version
Python 2.7.12
maddoxw#firefly:~$ python3 --version
Python 3.5.2
maddoxw#firefly:~$ which pip
Since I removed my ~/.local/bin directory from my path, then I can be certain pip will not be found. Also, $PYTHONPATH is still empty. Now I create my virtualenv:
maddoxw#firefly:~$ mkdir test && cd test/
mkdir: created directory 'test'
maddoxw#firefly:~/test$ python3 -m venv .env
maddoxw#firefly:~/test$ source .env/bin/activate
(.env) maddoxw#firefly:~/test$ echo $PATH
/home/maddoxw/test/.env/bin:/usr/local/cuda-8.0/bin:/home/maddoxw/.node_modules_global/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/maddoxw/bin:/home/maddoxw/scripts
(.env) maddoxw#firefly:~/test$ echo $PYTHONPATH
(.env) maddoxw#firefly:~/test$ which python
/home/maddoxw/test/.env/bin/python
(.env) maddoxw#firefly:~/test$ python --version
Python 3.5.2
(.env) maddoxw#firefly:~/test$ which pip
/home/maddoxw/test/.env/bin/pip
Good. My ~/.local/ is still NOT on my $PATH, $PYTHONPATH is still empty, python points to the correct path and version, and pip is pointing to the correct location. Now lets try to pip install a fresh lib.
(.env) maddoxw#firefly:~/test$ pip install Cython
Requirement already satisfied: Cython in /home/maddoxw/.local/lib/python2.7/site-packages
Why is pip still looking in a non-$PATH path?
First, install pip3 to use with python3. You can install it with the following command and then use pip3 to install your packages.
sudo apt-get install python3-pip
[Solved]
When I initially set up my python2.7 environment way back when, I created for myself a handy little function wrapper around pip so that I wouldn't have to type out the --user every time I wanted to pip install
pip() {
if [ "$1" = "install" -o "$1" = "bundle" ]; then
cmd="$1"
shift
$HOME/.local/bin/pip $cmd --user $#
else
$HOME/.local/bin/pip $#
fi
}
I put this function in ~/.bash.d/bash_functions
and in my ~/.bashrc i added the line,
[ -f ~/.bash.d/bash_functions ] && source ~/.bash.d/bash_functions
So, although I removed $HOME/.local/ from my path. This wrapper function was still being called everytime I fired up a new terminal. Weather or not I was or was not in a virtualenv was irrelevant.
Solution?
Commented out (or delete completely) the function wrapper fixed it.
My system is running CentOS 6. I do not have admin access, so sudo is not available. I have Python 2.7.3 available, along with pip and virtualenv. I was hoping that I could use these to set up a new virtual environment in which to install & run Python 3.5 or above.
I tried the method described here:
Using Python 3 in virtualenv
But got this error:
$ virtualenv -p python3 venv
The path python3 (from --python=python3) does not exist
My system also has a Python 3.4 module installed, so I tried that, however virtualenv does not seem to work there:
$ module load python/3.4.3
$ virtualenv -p python3 venv
-bash: virtualenv: command not found
This appears to make sense since virtualenv is only installed for Python 2.7:
$ module unload python
$ module load python/2.7
$ which virtualenv
/local/apps/python/2.7.3/bin/virtualenv
So, the next logical step would seem to be installing virtualenv for my Python 3... but this does not work either:
$ pip3 install virtualenv
Traceback (most recent call last):
File "/local/apps/python/3.4.3/bin/pip3", line 7, in <module>
from pip import main
ImportError: cannot import name 'main'
also
$ pip3 install --user virtualenv
Traceback (most recent call last):
File "/local/apps/python/3.4.3/bin/pip3", line 7, in <module>
from pip import main
ImportError: cannot import name 'main'
I started Google'ing this new error message, but did not see anything that seemed relevant for this situation. Any ideas? Even if I could get virtualenv installed on my Python 3.4 module, would I still be unable to upgrade it to Python 3.5+?
To round things out, I also tried to compile my own Python 3.6 from source, but that does not work either:
Python-3.6.0$ make install
if test "no-framework" = "no-framework" ; then \
/usr/bin/install -c python /usr/local/bin/python3.6m; \
else \
/usr/bin/install -c -s Mac/pythonw /usr/local/bin/python3.6m; \
fi
/usr/bin/install: cannot create regular file `/usr/local/bin/python3.6m': Permission denied
make: *** [altbininstall] Error 1
more background info:
$ which pip3
/local/apps/python/3.4.3/bin/pip3
$ which python
/local/apps/python/3.4.3/bin/python
You can download miniconda or Anaconda. It does not require superuser privileges because it installs in your home directory. After you install you can create new environments like this:
conda create -n py35 python=3.5
Then you can switch to the new environment:
source activate py35
Try this for Windows.
virtualenv -p C:\Python35\python.exe django_concurrent_env
cd django_concurrent_env
.\Source\activate
deactivate
Try out the following commands:
pip3 install virtualenv
pip3 install virtualenvwrapper
mkdir ~/.virtualenvs
export WORKON_HOME=~/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh
source ~/.bash_profile
which python3
Now copy the result of path of python3 in the last command and put it in the following command:
mkvirtualenv --python=python3/path/in/last/command myenv
I'm assuming pip3 is already installed. If not, install it before running these commands.
Source: https://docs.coala.io/en/latest/Help/MAC_Hints.html#create-virtual-environments-with-pyvenv
(I do have sudo access on my machine. I've not tried the commands without it. Please post if any issues comes.)
Since you already have virtualenv installed, you might only need to update the files and then run the command mkvirtualenv with proper arguments.
If you should ever encounter the following error when creating a Python virtual environment using the pyvenv command:
user$ pyvenv my_venv_dir
Error: Command '['/home/user/my_venv_dir/bin/python', '-Im', 'ensurepip', '--upgrade', '--default-pip']' returned non-zero exit status 1
... then the answer (below) provides a simple way to work around it, without resorting to setuptools and it's related acrobatics.
Here's an approach that is fairly O/S agnostic...
Both the pyvenv and python commands themselves include a --without-pip option that enable you to work around this issue; without resorting to setuptool or other headaches. Taking note of my inline comments below, here's how to do it, and is very easy to understand:
user$ pyvenv --without-pip ./pyvenv.d # Create virtual environment this way;
user$ python -m venv --without-pip ./pyvenv.d # --OR-- this newer way. Both work.
user$ source ./pyvenv.d/bin/activate # Now activate this new virtual environment.
(pyvenv.d) user$
# Within it, invoke this well-known script to manually install pip(1) into /pyvenv.d:
(pyvenv.d) user$ curl https://bootstrap.pypa.io/get-pip.py | python
(pyvenv.d) user$ deactivate # Next, reactivate this virtual environment,
user$ source ./pyvenv.d/bin/activate # which will now include the pip(1) command.
(pyvenv.d) user$
(pyvenv.d) user$ which pip # Verify that pip(1) is indeed present.
/path/to/pyvenv.d/bin/pip
(pyvenv.d) user$ pip install --upgrade pip # And finally, upgrade pip(1) itself;
(pyvenv.d) user$ # although it will likely be the
# latest version already.
# And that's it!
I hope this helps. \(◠﹏◠)/
2020, on python3.8 on WSL2 (Ubuntu) the following solved this for me:
sudo apt install python3.8-venv
If you have a module whose file is named after a python standard library, in the folder on which you invoke python -m venv venv, then this command will fail without a hint about that.
For instance, you name a file email.py.
What I did to find this was coding a bash script which moves the .py files off the current directory one by one (to a holdspace/ subdir), and try at each move to invoke the venv dir creation. If the python -m venv venv command exits with 0 code, so it's successful and the last file moved was the culprit.
#!/bin/bash
if [ ! -d ./holdspace ]
then
mkdir holdspace/
fi
for file in *.py
do
mv "$file" holdspace/
python -m venv venv >/dev/null 2>&1
if [ $? -eq 0 ]
then
echo "$file was the culprit."
rm -rf venv/
break
else
echo "$file isn't the culprit."
fi
rm -rf venv/
done
mv holdspace/*.py .
The following should fix it
brew update
brew upgrade
brew install zlib
I've been trying to get up and running with the built-in "venv" module of Python 3.3 on my OS X machine. I've installed Python 3.3 using Homebrew.
As per the docs, creating and switching virtual environment works as you'd expect:
$ python3 -m venv myvenv
$ source myvenv/bin/activate
And I've tested something like this:
$ echo "YEAH = 'YEAH!'" > myvenv/lib/python3.3/site-packages/thingy.py
$ python
>>> import thingy
>>> print(thingy.YEAH)
'YEAH!'
But when I try to install distribute, it simply won't go in the proper place. For some reason, it insists on trying to install into /usr/local/lib/python3.3/site-packages/, which fails with the following messages:
No setuptools distribution found
running install
Checking .pth file support in /usr/local/lib/python3.3/site-packages/
/Users/victor/myvenv/bin/python -E -c pass
TEST FAILED: /usr/local/lib/python3.3/site-packages/ does NOT support .pth files
error: bad install directory or PYTHONPATH
You are attempting to install a package to a directory that is not
on PYTHONPATH and which Python does not read ".pth" files from. The
installation directory you specified (via --install-dir, --prefix, or
the distutils default setting) was:
/usr/local/lib/python3.3/site-packages/
and your PYTHONPATH environment variable currently contains:
''
This happens regardless if I try to install using distribute_setup.py or using the source distribution directly. I've even tried using --prefix=/Users/victor/myenv but it still tries to put everything in my "global" site-packages.
I can't figure out why this happens, but it's consistent on two of my machines. Note that sys.prefix reports the correct path (the virtual environment).
Is this a problem with Homebrew? OS X? Python 3.3? venv? Me?
This has been an issue with Homebrew, yes, but it is working now since https://github.com/mxcl/homebrew/commit/0b50110107ea2998e65011ec31ce45931b446dab.
$ brew update
$ brew rm python3 #if you have installed it before
$ brew install python3
$ cd /tmp
$ which python3
/usr/local/bin/python3
$ python3 -m venv myvenv
$ source myvenv/bin/activate
$ wget http://python-distribute.org/distribute_setup.py # may need brew install wget
$ python3 distribute_setup.py
...
Finished processing dependencies for distribute==0.6.45
After install bootstrap.
Creating /private/tmp/myvenv/lib/python3.3/site-packages/setuptools-0.6c11-py3.3.egg-info
Creating /private/tmp/myvenv/lib/python3.3/site-packages/setuptools.pth
You see that distribute install successfully into the /tmp dir.
This happens because homebrew installs distutils config file:
$ brew cat python3 | grep "Tell distutils" -A5
# Tell distutils-based installers where to put scripts
(prefix/"Frameworks/Python.framework/Versions/#{VER}/lib/python#{VER}/distutils/distutils.cfg").write <<-EOF.undent
[install]
install-scripts=#{scripts_folder}
install-lib=#{site_packages}
EOF
$ mv ~/.local/Frameworks/Python.framework/Versions/3.3/lib/python3.3/distutils/distutils.cfg ~/tmp/
$ cat ~/tmp/distutils.cfg
[install]
install-scripts=/Users/gatto/.local/share/python3
install-lib=/Users/gatto/.local/lib/python3.3/site-packages
$ . venv/bin/activate
(venv) $ python distribute-0.6.36/distribute_setup.py
(venv) $ ls venv/lib/python3.3/site-packages/
distribute-0.6.36-py3.3.egg easy-install.pth setuptools-0.6c11-py3.3.egg-info setuptools.pth
See "distutils.cfg Can Break venv" issue at bugs.python.org.