python: install two versions of same module - python

To be more precise, I need to install two versions of Pandas. On one hand, I'm writing codes to be run on a server with pandas 0.13. All other part of my work, I want up-to-date pandas and other modules (0.16.1 for now).
The two projects are not connected and I won't need two versions in one program.
Is there a way to do that?
Edit: I'm using Python 2.7.8 with Anaconda under Windows

The best method is virtualenv. Virtualenv is a tool to create isolated Python environments.
http://virtualenv.readthedocs.org/en/latest/

I would highly recommended miniconda, which is the smaller version of Anaconda. Conda is a package manager which makes installing scientific libraries such as Scipy and Numpy easy. To get it, just install the Miniconda installer.
“Miniconda” only contains Python and conda, and is much smaller than
a full Anaconda installer. There are two variants of the installer:
Miniconda is based on Python 2, while Miniconda3 is based on Python 3.
Once Miniconda is installed, you can use the conda command to install
any other packages and create environments (still containing any
version of Python you want). If you have a slow internet connection or
limited disk space, Miniconda is the way to go.
It is fast to install packagaes such as Pandas and Numpy because many have been precompiled.
On OS X, the latest Python 2 version can be found here and is installed as follows:
$ bashMiniconda-latest-MacOSX-x86_64.sh -p /usr/local/miniconda -b
$ export PATH=/usr/local/miniconda/bin:$PATH
$ which conda
/usr/local/miniconda/bin/conda
$ conda --version
conda 3.7.0
Once Miniconda is installed, you can use the conda command to install any other packages and versions, and create environments, etc. For example:
$ conda install pandas=0.16.0
...
$ conda create -n py3k anaconda python=3
...
Two versions of the same package cannot run simultaneously, so I would recommend setting up a copy of your existing environment and then installing the desired version.
conda list will show all of your installed packages.

Use pkg_resourcesto force the version:
import pkg_resources
pkg_resources.require("YOUR_PACKAGE==VERSION")
import YOUR_PACKAGE

Related

Install library packages for other python version

I have installed the two python versions v2.7 and v3.7 on the same machine. I want to install the some packages for Python 3.7 when i am trying to install it installing for python2.7. Can any one help here how can i install the packages for python3.7.
Installed /usr/lib/python2.7/site-packages/configlib-2.0.4-py2.7.egg
Processing dependencies for configlib==2.0.4
Searching for everett==0.9
Reading https://pypi.python.org/simple/everett/
[root#nodehost configlib-2.0.4]# /usr/local/lib/python3.7 setup.py install
bash: /usr/local/lib/python3.7: Is a directory
[root#nodehost configlib-2.0.4]#
A more elegant solution is to create a separate environment for different python. Anaconda is the way to go. It is free and it installs many everyday libraries already.
Download and install the python 3.7 version. And then create a python 2 version is simple:
conda create -n py27 python=2.7
Every time you need the 2.7, just
conda activate py27
And then you can install libraries there just for 2.7, without messing up your 3.7. Vice versa for 3.7.
You should be able to choose your Python version with the two following commands:
>> python --version
Python 2.7
>> python3 --version
Python 3.7
If this works, just call pip like so:
>> python3 -m pip install everett

python cannot import graph-tool after installing it on a mac using brew (homebrew)

I used homebrew to install graph-tool on a macOS 10.14 using the command brew install graph-tool, but I cannot import it in python (that is installed through anaconda in /opt/anaconda3/bin/python). How can I make Python able to import graph-tool?
Prefer Conda Packages
If you want to use something in a Conda environment, then the best practice is to install through Conda. In this case, the graph-tool package is available through Conda Forge and can be installed with
conda install -c conda-forge graph-tool
Personally, since I started using Conda in 2016, I've found no reason to use Homebrew or MacPorts. There might be some exceptions, but this package isn't one.
conda install not working
Sometimes the packages you already have installed can conflict with the ones you want to add. This is especially the case when it comes to massive envs like the base in Anaconda. Best practice for using Conda is to avoid installing in base and instead create envs for each project or project type. This also helps you avoid breaking code you've already written because sometimes installs can trigger a version change in a package you were using.
In this case, you could make a new env, say foo, with
conda create --name foo -c conda-forge graph-tool
You can list any additional packages you know you'll need after graph-tool.

How can I change between versions of python 3

I am trying to use tensorflow but my python is to recent. I have python3.7.2 and I need py3.6 in order to install and use tensorflow.
I have installed py3.6.8 but I still can't install it with pip. Is there a way of interchanging between versions of python to install/use tensorflow. Or is it to do with my pip version?
The error is:
Could not find version that satisfies the requirement tensorflow in versions:
The main problem is that I don't know how to get tensorflow. Can someone help me do this?
By far the best option will be to use Anaconda virtual environment. After you install Anaconda, use environments to manage different versions of Python:
Python 3.6.8:
conda create -n myenv python=3.6.8 tensorflow
Python 3.7:
conda create -n myenv python=3.7 tensorflow
Why am I saying it's best with Anaconda? Long story short, it can be (much) faster. Here's an article that discusses why.
Option 1:
Install multiple versions in separate directories, and then you run the python program with the Python version you want to use. Like so:
C:\Python26\Python.exe thescript.py
What virtualenv does is that it gives you many separate "virtual" installations of the same python version. That's a completely different issue, and hence it will not help you in any way.
Option 2:
Use Pythonbrew.
Once pythonbrew is installed:
#to install new python versions is as simple as:
pythonbrew install 2.7.2 3.2
#to use a particular version in the current shell:
pythonbrew use 3.2
#to uninstall:
pythonbrew uninstall 2.7.2

Python - package not found although it is installed

I have the following version of python
import sys
print(sys.version)
3.6.5 | packaged by conda-forge | (default, Apr 6 2018, 13:44:09)
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)]
I installed a package with the following command
pip install wfdb
It is succesfully installed because when I then write the command:
pip show wfdb
The following information appears
Location:
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages
However, when I type the command import wfdb in Python notebook or the version of python in terminal, I get the following message
No module named 'wfdb'
Does it have to do with the path on which python is checking where the packages are? How to check this and how to change it?
You have (at least) 2 Python installations, one managed by Anaconda, the other what appears to be an official Python.org Mac build installed system-wide. The pip command on the command-line is the one tied to the Python.org Mac build.
pip is a script that is tied to a specific Python installation, and there can be multiple versions of the script installed in different locations, and is usually also installed with pipX and pipX.Y to match the X.Y version indicator of the Python version it is tied to. For Python 3.6, that means the same script would also be available as pip3 and pip3.6. (This also means that pip can be connected to Python 2 or Python 3, depending on your exact OS setup. It is not a given that pip, without a version number, installs into Python 2.x as some answers may claim).
Note that when you run a command without a path in your shell, (such as pip as opposed to /usr/bin/pip), you are asking your shell to find the command for you in a number of locations, listed in the PATH environment variable. The first location in the PATH list with that command is then fixed. which -a <command> would tell you all possible PATH-registered locations that the command can be found in. You can always use the full path to a command to bypass the PATH search path.
You can always verify what Python version the pip command is connected to with:
pip -V
which will output the version of pip and the location it is installed with. It'll print something like
pip pipX.pipY path/to/pythonX.Y/site-packages/pip (python X.Y)
where pipX.pipY is the pip version number and path/to/pythonX.Y tells you what Python installation this is for.
You can try to match this with the Python version by running
python -m site
which outputs the Python module search path for that Python version. Python can be run with python, pythonX and pythonX.Y too, and is subject to the same PATH search.
Note the -m switch there, that instructs Python to find a module in it's module search path and execute it as a script. Loads of modules support being run that way, including pip. This is important as that helps avoid having to search for a better pip command if you already can start the right Python version.
You have several good options here:
Since you are using Anaconda, you could look for a conda package for the same project. There is such a package for wfdb. Install it with
conda install wfdb
Anaconda aims to give you a wider software management experience that includes a broader set of software options than just the Python PyPI ecosystem, and conda packages usually manage more things than just the Python package.
Conda packages are usually maintained by a different set of developers from the package itself, so there may be a newer version available on PyPI (requiring pip install) than there is on Conda.
This is not an option for all Python packages, if there is no conda package you have to use pip. See Installing non-conda packages.
you can use the conda command to create a conda environment. Once you have an environment created, you can activate it with
source activate <name_of_cenv>
to alter your PATH settings. With the envirnoment 'active' the first directory listed on your PATH is the one for the conda environment and the pip command will be the one tied to that environment.
Note that a conda environment gives you an isolated environment for a specific project, keeping the library installation separate from the rest of your Python packages in the central site-packages location. If you want to install a package for all of your Anaconda Python projects, don't use a conda environment.
Use the Anaconda Python binary to run pip as a module; when you can run /path/to/python or pythoncommand to open the right Python version, you can use that same path to run /path/to/python -m pip ... instead of pip ... to be absolutely certain you are installing into the correct Python version.
Look for a better pip command, with which -a pip or which -a pip3.6, etc. But if you already know the Python binary, look in the same bin location for pip. If you have anaconda/bin/python, then there probably is a anaconda/bin/pip too.
As you can read here:
pip3 and pip would make a difference only when you are not using any
environment managers like virualenv (or) conda. Now as you are
creating a conda environment which has python==3.x, pip would be
equivalent to pip3.
For this reason it could be you did not activate your Conda environment before installing required packages and running your code.
Activate the new environment:
On Windows:
activate myenv
On macOS (this should be your option) and Linux:
source activate myenv
NOTE: Replace myenv with the name of the environment.
which python
gives the you the PATH to python
and then /path/to/python -m pip install thepackagetobeinstalled
Many thanks #MartijnPieters
You have installed python2.x package and you're using python3.x. Try:
pip3 install wfdb
If you don't have pip3 run:
[apt-get/yum] install python3-pip
You can see what packages you have currently installed by running:
pip freeze
and for python 3.x packages
pip3 freeze
Please remember each time you install a Python package, it will be placed in the directory for one particular Python version. Hence your error.

How to install numpy and matplotlib in the right python version?

I'm trying to install numpy, matplotlib, and scipy in the right python version.
Initially I was testing with different python versions (3.2, 2.7, 2.6).
I removed all these versions using: How to uninstall Python 2.7 on a Mac OS X 10.6.4?
Afterwards, I reinstalled Python 2.7.11.
when I try to install numpy, matplotlib and scipy, using pip, I get the following message:
Requirement already satisfied (use --upgrade to upgrade) ...
In my terminal, I tried the following:
$ which python
/Library/Frameworks/Python.framework/Versions/2.7/bin/python
$ python
Python 2.7.11 (v2.7.11:.....)
.....
>>> import numpy
ImportError: No module named numpy
$ /System/Library/Frameworks/Python.framework/Versions/2.7/bin/python
Python 2.7.10 (default, ......
.....
>>> import numpy
>>> numpy.__version__
'1.8.0rc1'
for some reason these packages got installed in 2.7.10 and not 2.7.11, which is the version I downloaded from python.org. Also, I don't even know how I got the 2.7.10 version.
How can I fix this issue?
You can also use macports (https://www.macports.org/) to install different versions of python, numpy, and matplotlib. It's really quite simple.
Alternatively, you can perhaps use anaconda (https://www.continuum.io/downloads), which uses conda, to achieve your goal.
I recommend using virtualenv (with virtualenvwrapper: https://virtualenvwrapper.readthedocs.org). It is very easy to setup and you'll have absolutely no problems in future when you deal with multiple Python installations.
I work with virtualenv for years now and create for each project a separate virtual environment, which is always clean and I never have to deal with PATH, PYTHONPATH or whatever.
If you followed the virtualenvwrapper installation guide, you can simply create for example one virtualenv for everyday work via:
mkvirtualenv common -p /Library/Frameworks/Python.framework/Versions/2.7/bin/python
this will create the virtualenv and automatically activate it, so you can instantly install the packages you want:
pip install matplotlib numpy scipy
and every time you want to use it you type:
workon common
As you see above, you can specify the python executable via the -p flag. Each virtualenv will be a completely fresh and independent Python installation where you can use pip to install whatever you want (without root access of course).
It is likely to mean that you used pip or easy_install from another python version.
When you install your modules, make sure to use the correct pip version.
It might be /usr/local/bin/pip2.7 for example.
If you install Anaconda from continuum.io, you'll get access to versions of many packages that have been tested to work with the version of Python that you are interested in working with. Here's the list that come with the current version of their distribution.
You also get access to conda, which is a package and environment manager. Think pip + virtualenv.
Once you have that, you can do
conda create -n my_env python=3.6 numpy pandas
This will install Python 3.6 and all of the dependencies for numpy and pandas into a virtual environment called my_env. Conda will make sure that you have the most up to date packages that work together.
To access your environment, you can do:
activate my_env
Now you're running Python in that environment with those installed packages. If you need more packages, you can either do conda install package_name. If conda can't find the package, you can still do pip install package_name.
Note that as an added bonus, you get an optimized and pre-compiled version of Numpy by way of the Intel MKL.
(From my comment on a previous answer)
I'd second the recommendation for going the Anaconda route.
Particularly if you're using Numpy or anything that depends on Numpy
(Pandas, Scipy, Sci-kit Learn). Continuum has access to the Intel MKL
which gives you significant optimizations and pre-compiled C code
specific to your operating system. docs.continuum.io/mkl-optimizations

Categories