Can you install a Python package via R - Reticulate - python

I am about to create a python interface in R with the package Reticulate. In order to access the python functions in R, the respective python packages need to be installed.
Two questions came to my mind:
1) If you use the reticulate package, does the Anaconda package need to be installed? Or is it sufficient to install the python packages only?
2) Is it possible to install python packages in R, similar to install.packages("r_package")?
Does anyone have experience with this Topic? Thanks in advance!

I'll add a little bit of nuance to the previous answer.
Like #f0nzie said, Anaconda is not a package, but a package manager. Ideally, you will create an environment using Anaconda to assist with your package management and version control. The documentation for conda environments is here.
Now, you can install python packages to your anaconda package in R. That is possible using reticulate::conda_install(envname, packages). The documentation for conda_install() can be found here.

1) The R package reticulate can work with the default python or with Anaconda2 or Anaconda3. If you want Anaconda to work with R, you will have to install Anaconda first. Once installed, you call the library(reticulate), and run py_config() or reticulate::py_discover_config(), that will give you the list of paths and environment used by the Python installation. Then, once you know the Python path, you add a line like this use_python("/opt/miniconda2/bin/python"), right after library(reticulate) and you are in business.
2) to install Python packages so R (or reticulate) can see them, you have to install them as regular Python packages from a terminal or console; not R. Example: conda install numpy to install numpy, or conda install scipy to install scipy, and so on.
I am just doing all this in a Docker container rocker/rstudio. It should be easier in a standard OS.
Here is step-by-step instructions: rstudio reticulate
Cheers!

In case you need a specific version of Python modules then place == after module name, e.g. the following will install specific versions of 3 modules using pip:
reticulate::conda_install(c("PyMuPDF==1.14.20", "PyPDF2==1.26.0", "reportlab==3.5.23"),
envname = "myenv", pip = TRUE)

Related

Does installing anaconda conflict with Python3 and R already installed?

On my Ubuntu 18.04, I have Python 3 and R installed. I am about to study some data science, and just found https://www.anaconda.com/distribution/.
anaconda comes with Python and R and some packages. Will installing anaconda conflict with my existing installation of Python 3 and R?
Shall I install anaconda, or shall I install the packages manually and individually on demand?
How do people in data science install the tools?
Thanks.
I have a system python and R as well as anaconda and they don’t seem to conflict and I have the same OS as you. Conda, the package and environment manager that comes with anaconda, supposedly does not mix well with pip, meaning if you have a virtual environment you should use one or the other. However, I have mixed them without any difficulties.
I prefer creating virtual environments the more standard way, but there are some scientific packages that are much easier to install using conda.

How to delete extra pythons on Mac OS X Sierra

In the usr/bin folder, there are three versions of Pythons installed: Python, Python2.6, Python2.7 (the folder names) - not sure what version is for Python folder.
My issue, originally, was that I tried to install the module 'pandas' to run a script, python keeps telling me pandas could not be found.
'pip freeze' shows me pandas is already installed.
However, I could not find pandas using python>>help>>modules.
So I suspected there are multiple pythons installed causing pip installing for one of them, but the default python is a different one.
So my questions are -
1 Which python is the default one that comes with macOS Sierra? (I can confirm pandas currently is installed for Python, not Python2.6 or Python2.7)
2 Can I remove extra Pythons that do not have pandas?
3 How can I find what it the default Python when I type 'Python...' and how to install pandas for that python?
Solved:
Thanks for the comments and reply.
I used "python2.7 install pip" to install pip for python2.7. Then I used command 'pip2.7 install pandas'. This way, pandas is installed for the default python. (The command 'pip install pandas' on my machine, is installing for python 2.6.)
macOS Sierra uses Python 2.7 by default.
You can uninstall a version of Python as described here. However, you shouldn't need to as long as you are managing your packages and environments. If you want to maintain more control over your projects and their packages/versions, you should take a look at virtualenv. Virtualenv creates environments that have their own installation directories that don't share packages with other virtualenv environments. This is a very popular option many people use when managing projects.
You can use pip --version to see which version of Python you are installing a package for. You can also use pip list to see which packages are currently installed.
I highly recommend taking a look at virtualenv as it'll make keeping track of Python environments and their respective packages a lot easier.
Hope this helps!

Python Anaconda - no module named numpy

I recently installed Anaconda on Arch Linux from the Arch repositories. By default, it was set to Python3, whereas I would like to use Python2.7. I followed the Anaconda documentation to create a new Python2 environment. Upon running my Python script which uses Numpy, I got the error No module named NumPy. I found this rather strange, as one of the major points of using Anaconda is easy installation of the NumPy/SciPy stack...
Nevertheless, I ran conda install numpy and it installed. Now, I still cannot import numpy, but when I run conda install numpy it says it is already installed. What gives?
Output of which conda: /opt/anaconda/envs/python2/bin/conda
Output of which python: /opt/anaconda/envs/python2/bin/python
The anaconda package in the AUR is broken. If anyone encounters this, simply install anaconda from their website. The AUR attempts to do a system-wide install, which gets rather screwy with the path.

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

Python update from 2.7.6 to 2.7.8 - did I just lose all my previously installed modules?

Title basically states it all. I upgraded my version of Python in order to hopefully play more nicely with Mac OS 10.9, but am now unable to use some modules I need for my work (NumPy, Pandas, SciPy, Scikit-Learn, etc.) Does this upgrade automatically wipe out any previously installed modules? Do I just need to install them again? Thanks in advance.
When you upgraded, it created a new sitepackages directory structure. Your packages are not installed any more, so yes you need to reinstall them into the new version.
Before you do that, take a good look at virtual environments rather than install the modules and packages globally.
http://docs.python-guide.org/en/latest/dev/virtualenvs will get you started, then google virtualenvwrapper.
I would recommend you try out the anaconda python distribution. It comes with all of these packages pre-installed, and its free. Also, in addition to pip, you can use the conda package manager which is much better for scientific packages. See http://technicaldiscovery.blogspot.com/2013/12/why-i-promote-conda.html for an explanation.
With conda, you can install numpy/scipy/pandas/etc with conda install numpy scipy pandas and it just works, and takes about 10 seconds. No compilation necessary (OTOH pip install scipy can take over 15 minutes, requires a fortran compiler, and is generally very tricky).
link: http://continuum.io/downloads

Categories