How to setup pip for coexisting Python 2.7/3.4? - python

I am using Anaconda Python 3.4 on a Windows 7 PC now. Recently I am trying to follow the instruction of the book High Performance Python to learn some profiling skills. To this end I need to use pip install to install several tools. Unfortunately, not all of them support Python 3, and I have to install Python 2.7 now.
Before installing Python 2.7, I would like to know how I should handle with such 2.7/3.4 coexisting system? How do I setup pip so that I could use pip install to install packages for different Python versions separately?

You can create a conda environment via:
conda create --name py27 python=2.7
and use this environment for your work with Python 2.7. Activate it with the command activate py27, going back to your root environment is just activate.
In the py27 environment you can install pip and all other packages you need.

pip is generally located at the Python27\Scripts and/or Python34\Scripts folder. If you wish to invoke pip directly in the command line, these folders should be in your PATH environment variable.
Now I would just rename pip.exe in Python34\Scripts into any other name, for example pip_for_3.exe. That way, when I install packages for Python27, I would just use:
pip install <package name>
and packages for Python34:
pip_for_3 install <package name>
Coexisting Python installations are not a problem, you just have to know which version is invoked every time. See this answer for the same idea.

Related

Setting up virtual environments for python projects

I am trying to properly install python and set up virtual environments to make project development easier. Im stuck on how to install pip now. My question is this:
Do I need to install pip in a specific way if I'm trying to set up virtual environments?
Here's what I've done so far...
installed pyenv
used pyenv to install python version 3.7, 3.8 and 3.9
set global pyenv to be python 3.9
Now I don't know hot to set up pip.
I did this:
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
and then:
python get-pip.py
..however when I go to verify pip was installed i get 'pip command not found'.
How do I properly set up pip so that I can create virtual environments and install packages for different environments?
Do I need to add pip to my PATH? If so, how do I do that?
You can create virtual environment directly from python with venv
(documentation: https://docs.python.org/3/library/venv.html)
Example:
python3 -m venv /path/to/new/virtual/environment
Activate the virtual environment
source /path/to/new/virtual/environment/bin/activate
Then you can install any package with
pip package-to-install

Install different versions of python 2

My system is ubuntu 18.04.
I have a pre-installed version 3 and 2 of python.
which python3
/usr/bin/python3
python3 -V
Python 3.6.9
which python
/usr/bin/python
python -V
Python 2.7.17
I need to create several virtual environments, one for python 2.7.15 and another for 2.6. how can I do that?
There are different ways of creating virtual python environments. Three popular ones are
virtualenv
pipenv
conda
I personally like conda a lot.
virtualenv
Assuming you have pip installed, you get virtualenv with
pip install virtualenv
Once installed, you can change into a directory of your choice and create a virtual environment like this
virtualenv myenvironmentname
If you want to use a different python version in your virtual environment, you can specify this with the --python flag.
virtualenv --python=/usr/bin/python2.6 myenvironmentname
However, please note that this requires you to have the python version you specify installed in advance, virtualenv will not take care of that for you (have a look at Use different Python version with virtualenv for more details). So you'll need local installations of the versions you desire.
You then can activate the environment with
myenvironmentname/bin/activate
and go ahead to use pip to install packages, etc. Have a look at
pip freeze --help
to find out on how to make your environment reusable.
pipenv
pipenv combines pip and virtualenv.
You can install it using
pip install --user pipenv
Pipenv takes care of dependencies on a project basis
cd myprojectfolder
pipenv install
This will create a Pipfile which will track dependencies and a virtualenv (see https://docs.python-guide.org/dev/virtualenvs/ for more details).
To create an environment using a specific version, you can do
pipenv install --python '/usr/bin/python2.6'
or
pipenv install --python 2.6
Cmp. Set python version when creating virtualenv using pipenv. If you also have pyenv installed, the second form will prompt pipenv to attempt to install non-existing versions, afaik.
conda
Anaconda Python is a python distribution (with a focus on data science) that comes with its own package and virtual environment manager named conda. Anaconda Python is not available in the official package repository of Ubuntu 18.04 LTS but needs to be installed in another way (the official documentation can be found here: https://docs.anaconda.com/anaconda/install/linux/).
To create an environment with conda, do
conda create --name myenvironmentname python=2.7.15
In contrast to virtualenv, the environments are by default not created in the present working directory, but installed into the envs directory in your conda directory. conda will also take care to install the proper python version, that is at least as long as it is part of the default channel (see below).
You can then activate said environment with
conda activate myenvironmentname
As I wrote above, the python version you specify needs to be available from the configured conda channels. python2.6 however, was removed from the default channel. To remedy this, you can add the free channel back to your default list (see https://docs.conda.io/projects/conda/en/latest/user-guide/configuration/free-channel.html for more details):
conda config --set restore_free_channel true
After that you can
conda create --name myotherenvironmentname python=2.6
And switch between the environments as you like
conda activate myotherenvironmentname
For python3 python -m venv <your_virtual_enviroment_path> for python2 virtualenv <your_virutal_enviroment_path>
The to activate source <your_virtual_environment_path>/bin/activate. And to deactivate deactivate. Finally to check what is activated echo $VIRTUAL_ENV
I strongly recommend for one virtual environment for each project.

Installed standalone Python 3.7 after Anaconda Python 3.7, how to access old/specific pip?

I'm relatively new to Python. I installed 3.7 as part of Anaconda package, but it wasn't working with PowerBI since it wasn't able to execute a certain batch file that started the conda venv. A workaround I read about was to install a regular Python 3.7 outside of conda and use that interpreter instead.
It successfully installed, and it was added to path, and when I do pip-list in my command prompt I see only the packages for the new pip which makes sense. How do I access the old pip for my conda python where it had all the packages?
Do you mean how do you install packages in your default Python environment outside of Conda? To achieve this you'd just run pip install to install the packages fresh. If you want the list of previously install items you can use pip list.
conda activate myoldenv
pip freeze > requirements.txt
You can then open requirements.txt to see which packages that you want.
However if you want PBI to work well with Python the cleanest way in my opinion is to setup within a new conda env. The point of Conda is to have different envs for different use cases.
conda create --name VisualisationPBI python=3.7
conda activate VisualisationPBI
pip install seaborn #installs seaborn and dependencies including numpy, pandas and mpl
In PBI options set your Python home directory to "Other" then select your conda directory, probably:
C:\Users\YOURNAME.conda\envs\VisualisationPBI
Conda works with PBI, but you need to Pip install the key packages in the library, instead of conda install. As here: https://community.powerbi.com/t5/Desktop/Power-BI-Python-with-Anaconda-missing-dependency/td-p/665102

difference between "conda install python=3.6" and "apt install python=3.6"?

Context: I have python 3.7 on my computer (Linux). Some package (tensorflow) needs a previous version of python to function.
In this post, a user suggested to install a previous version of python using the command:
conda install python=3.6
I am confused by this command, since I normally install python using apt or apt-get ( I am on ubuntu ). I think of python as being separate from anaconda.
apt install python=3.6
What is the difference between these two commands?
What you might want to do if you need a specific version of Python for a particular project is making a 'virtual environment'. Basically, that means that pip packages are installed within the project folder rather than in your bin folder somewhere on your computer. Virtual environment can also link to a version of python using something like virtualenv --python=/usr/bin/python2.6.
apt install python=3.6 will install in the standard bin folder of your distro.
conda install python=3.6 will check in which environment you currently are and install it there. It of course requires Anaconda installed and setup on your computer.
There are a lot of virtual environment management packages out there and I am not going to give an opinion on which is the best.
Note that if you install it using apt install, the version used in command line for python3 or python may be ambiguous, to be sure, you can specify the full path or make an alias for that path if there isn't one.

Installing Custom Python

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.

Categories

Resources