So I heard about the proper way to install packages into python by creating a new virtual environment for every project. Being on a mac (10.8) I have installed python3 using Homebrew, then I installed pip and virtualenv on this copy.
Now here comes the problem:
I create a new virtualenv, and activate it using:
virtualenv testing
source testing/bin/activate
When I type
which python
/Users/mik/Desktop/testing/bin/python
But typing
which pip
/usr/local/bin/pip
(learned of this when trying to install a package in the virtual environment, and it installed in the system wide installation in /usr/local/)
Inside the folder testing there is no file referring to pip
Extra Question: How does pip know which python to install the files to, for example pip list (which I believe refers to python 2.7) outputs the names of packages installed on python 3.3
I'll start with the last question as it explains what is happening.
The commands pip and easy_install are python scripts which are made executable on the filesystem. The python they use is the python that the first line tells to run the script. e.g. in /usr/bin/easy_install it is #!/usr/bin/python This will be Apple's python. So easy_install will install the 2.7 version of pip and virtualenv and will ignore your python3.3 setup.
The way to instal into python 3 is to install the 3.3 version of pip and virtualenv, the easiest way would be to install the Homebrew package for them. I think it is easier and less confusing to use just one package manager (Homebrew here) and not two (i.e. Homebrew and python).
You can also install easy_install directly. The way to do this is install the distribute package using python3.3 explicitly.
Python 3.4 will make this much easier as pip will always be available
Related
I have just uninstalled some older python versions (3.9x, 3.10x) on windows 10 and reinstalled the latest python 3.10.7. My environment variables all reference the correct location for python, but when I run pip list I get a bunch of old packages when they should no longer be there. Since I uninstalled python with the intention to have a fresh installation, I don't want those old packages, but they are even still importable. If I run pip -v list, I see most of those (except the base packages that come with python) refer to ../appdata/roaming/python/python310/, instead of ..appdata/local/..., where Python is actually installed.
What can I do to clear this up? Sure I could pip uninstall them, but I don't know if this indicates a deeper issue and I am quite confused by it.
You get a ton of old packages because you didn't work with venvs sometimes! You can safely run this command to uninstall them all pip uninstall -y -r <(pip freeze) or either you can use your requirements.txt file to uninstall only specified libraries, as suggested in this thread.
Working with venvs fixes this exact problem. Every project will have its own environment with its own libraries to be installed. It may sound heavier (for example, if you have three projects that need requests, you will have to install requests three times) but you won't have any sort of version incompatibility since you can safely install different versions of the same package!
Always remember to set up the venv either using PyCharm or running these commands once you are in your folder:
python -m venv .
source venv/bin/activate
pip install -r requirements.txt
Either, if you will use Linux/Ubuntu, you can do apt install virtualenv, which also automatically creates a venv using this command:
virtualenv venv
Using venv also helps using a different Python version for each project meanwhile having them all installed! Here is how to change the version.
To exit a venv, just run the command deactivate!
To understand if you're in a venv, you have to check whether (venv) is written before your host in the cmd or in the PyCharm terminal, like so:
NOT IN VENV:
marco#marco-Aspire-A515-51G:~
IN VENV:
(venv) marco#marco-Aspire-A515-51G:~
So im pretty new to python and i need pip for something i wanna do, but everytime i install python the Scripts folder where pip should be always installs empty. I make sure that install pip is checked when downloading python. How do I get it to install pip with python?
If you installed Python, it came bundled with pip.
The default packages:
C:\Users\<Your username>\AppData\Local\Programs\Python\Python310\Lib\site-packages
A better practice is to install a venv for every project you are working on. Every venv will have its own set of dependencies(i.e. packages), and it will be easier to manage them.
You can find installed packages in your venv:
YOUR_PROJECT_PATH\venv\Lib\site-packages
EDIT
From official docs:
Pip not installed¶ It is possible that pip does not get installed by default. One potential fix is:
python -m ensurepip --default-pip
There are also additional resources
for Installing pip
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.
I am trying to install the latest (2.7.6) version of python on my ubuntu box that already has 2.7.4 installed through the package manager. I'm up for any solution that someone has for this, but am not quite sure how to do this myself.
I have used virtualenv to create virtual python setups for different django versions, but I don't know how to use virtualenv to create an environment with an updated python version (or if it is even possible).
So to install I downloaded the source and created a custom install using the below code
wget http://python.org/ftp/python/2.7.6/Python-2.7.6.tgz
tar -xvf Python-2.7.6.tgz
cd Python-2.7.6
./configure PREFIX=$SOMEBASE/python-2.7.6
make install DESTDIR=$SOMEBASE/python-2.7.6
This seems to work for the installation, however when trying to install a package on python I get the error that it can't write to /usr/local/lib/python2.7/site-packages. I could have it write there by running as root, but wasn't sure what that would do to my existing installation and really, really don't want to break what is already there. So I would love to know if there is a way (and how) I could specify a location for the site-packages to be used (like $SOMEBASE/python-2.7.6/Lib/site-packages).
Lennart Regebro has written instructions on how to install easy_install, virtualenv, and pip for a particular Python installation.
Alternatively, there is a shell tool called virtualenvwrapper which can automate much of the process. After installing python2.7.6, (and virtualenvwrapper), you'd type
cd ~/.virtualenvs
mkvirtualenv myenv -p /path/to/python2.7.6
to make a new environment called myenv. mkvirtualenv will install easy_install and pip for you. Once you activate myenv with
workon myenv
additional modules or packages which you install with easy_install or pip will use the right version of Python and will install the modules in ~/.virtualenvs/myenv/lib/python2.7/site-packages.
Normally you rn and build Python like this:
./configure --prefix=/wherever/python-2.7.6
make
sudo make install
You'll still have to sudo when installing modules, but that's good, prevents you from doing it by mistake. They will be installed to /wherever/python-2.7.6/lib/python2.7/site-packages.
I have python2.6 and django1.2.3 already installed on my system (Ubuntu 10.x).
This is the setup i use for most of my projects. But for some projects I need sandboxed environments, different django version, some extra python modules and sometimes even different python version.
So, I am trying to use pip and virtualenv now,but I am unable to change python and django version. Will I have to remove default setup and move all existing projects into 1 virtualenv. Can I avoid this? Even if I do that, how can I specify a different version of python?
If I have to remove the old settings. How do i do that? I have currently most of the things installed in /usr/local/lib/python2.6/dist-packages/ but I am not sure if there is anything installed anywhere else also.
If I have a completely blank setup with just Ubuntu, what is the ideal workflow? Is it this one?
Install python
$ sudo apt-get install python-setuptools
$ sudo apt-get install python-virtualenv
$ sudo easy_install pip
$ sudo pip install virtualenvwrapper
You want to do:
virtualenv --python=/path/to/python/version --no-site-packages ENV_NAME
For example:
virtualenv --python=/usr/bin/python2.6 --no-site-packages my_project_env
If you follow this for your projects you should be able to have a separate configuration for each one.
I have installed every Python verison I need (which is 2.4, 2.5, 2.6, 2.7, 3.1 and also 3.2) from source. That's always the best thing to do, so you don't mess up the system Python.
I installed them in /opt. Like so (you need a bunch of Ubuntu packages too, first):
./configure --prefix /opt/pythonxx
make -j2; make install # j2 is a nice trick there for dualcores not everyone knows.
Then I for each version install the things I need. I start with installing Distribute:
wget http://nightly.ziade.org/distribute_setup.py
/opt/pythonxx/bin/python distribute_setup.py
(Except for Python 3, who needs distribute_setup3.py)
Then I can install pip
/opt/pythonxx/bin/easy_install pip
And virtualenv:
/opt/pythonxx/bin/pip install virtualenv
(Virtualenv3 or virtualenv5 for Python 3)
And that's it! If I want to make a virtualenv using Python 2.4, I do:
/opt/python24/bin/virtualenv foobar
And Python 2.7:
/opt/python27/bin/virtualenv foobar
Running python is just
/opt/python24/bin/python
Etc. I never install anything in the above Pythons except these modules, and PIL (because PIL is a pain, but now there is Pillow, so you don't have to do that either). I use zc.buildout and virtualenv to keep the pythons clean.
You can use virtualenv --no-site-packages ENVNAME and that will make sure the default Django in your system Python won't be included in your new environment.
For different versions of Python, you can follow these instructions from a superuser.com post.