简体   繁体   中英

Installing both Python and R for a Travis build?

I've been working on an R package which interfaces with Python via a simple server script and socket connections. I can test in on my own machine just fine, but I'd like to test it on a Travis build as well (I don't want to go through the effort of setting up a Linux VM). To do this I would need a Python install that I can pass the path to in my R package tests, and a port number to use.

I've seen this answer which suggests installing multiple Python builds is possible, but I'm not sure how to approach

  1. Specifying the path(s) to the Python executable(s)
  2. choosing a port number for the test. It should also be noted that the Python script I am using for the Python 'server' uses 'localhost'.

Is it possible to do what I want on Travis? Should I want to do this with Travis?

EDIT Here's my Travis config:

language: r
r:
  - release
  - devel
cache: packages
sudo: false

matrix:
  include:
    - python:2.7
    - python:3.6

# Be strict when checking our package
warnings_are_errors: true

# System dependencies for HTTP calling
r_binary_packages:
 - jsonlite
 - R6

And here is the example in my R package:

pypath = Sys.which('python') 
if(nchar(pypath) > 0) {
  py = PythonEnv$new(port = 6011, path = pypath)
  py$start
  py$running
  py$set(a = 5)
  py$get('a')
  py$stop
} else {
  message("No Python environment available")
}

My example definitely finds a Python path, but fails with the error

Warning in socketConnection(port = self$port, open = "r+", blocking = TRUE, : localhost:6011 cannot be opened

Error socketConnection(port = self$port, open = "r+", blocking = TRUE, :
cannot open the connection

I tested this with another port and same error occurs.

EDIT 2

I've also tried it with host 127.0.0.1 and 0.0.0.0 but no dice. According to the Travis documentation, this should work... perhaps an issue with the R container?

Here is a travis.yml I use for my pyrle package. It just installs R usinq the ubuntu package manager:

language: python
python:
  - "3.6"
install:
  - pip install cython pytest hypothesis
  - sudo apt-get install -y r-base
  - echo 'source("https://bioconductor.org/biocLite.R"); biocLite("S4Vectors"); biocLite("GenomicRanges")' > install.R
  - python setup.py install

script:
  - py.test tests/

Another way is to install R through conda. Here is an example from the pyranges package:

# Stolen from http://conda.pydata.org/docs/travis.html
language: python
python:
  # We don't actually use the Travis Python, but this keeps it organized.
  - "3.6"
install:
  - sudo apt-get update
  # We do this conditionally because it saves us some downloading if the
  # version is the same.
  - wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh;
  - bash miniconda.sh -b -p $HOME/miniconda
  - export PATH="$HOME/miniconda/bin:$PATH"
  - hash -r
  - conda config --set always_yes yes --set changeps1 no
  - conda update -q conda
  - conda config --add channels bioconda
  - conda config --add channels r
  # Useful for debugging any issues with conda
  - conda info -a
  - conda create -q -n test-environment python=$TRAVIS_PYTHON_VERSION numpy scipy pandas pytest pytest-cov cython tabulate hypothesis bedtools # ray
  - source activate test-environment
  - python setup.py install # will install ncls
  - python --version
  - python -c 'import pandas as pd; print(pd.__version__)'
  - ls tests

script: py.test -v tests # verbose to see that tests run and so that travis does not time out on hypothesis tests

Your question title and question body are quite different.

Regarding the python+R Question

Similarly to The Unfun Cat's answer which uses travis to install python and installs R with apt, you can use travis for R and install python with apt:

language: r
install:
  - sudo apt-get install -y python2.7 python3.6

Note that travis also has an apt addon which makes your .yaml a bit cleaner (but arguably less portable).

language: r
addons:
  apt:
    packages:
    - python2.7
    - python3.6

The different python versions installed should be accessible to Sys.which as python2.7 and python3.6 instead of just "python". which python will return either the last version installed or else the last installed python2* .


Regarding the networking question

There are a number of reasons a you may be unable to establish a connection. A common reason is that the port is already in use. Port 6011 is sometimes used by the X window system ( ref ), so it is possible a one of travis's services is using it.

Using this reference on how to check for used ports you might try adding something like

sudo lsof -i -P -n | grep LISTEN | grep 6011

to your travis.yaml so you can see in the log if the port is used. You might also try a different unused port number in your script.

I did find this github comment referencing similar issues with R specifically; perhaps you can find more there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM