I have used knitr
for a long time, usually in the R Studio environment. Recently I installed Python (version 3.4.1) on my Windows machine, put it in the path, and tried out Yi Hui Xie's sample document for Python. But the Python code chunks won't run. From a chunk like this:
{r test-python, engine='python'}
x = 'hello, python world!'
print x
print x.split(' ')
I get an error message like this:
Warning: running command '"python" -c "print '**Write** _something_ in `Markdown` from `Python`!'"' had status 1
running: "python" -c "x = 'hello, python world!'
print x
print x.split(' ')"
File "<string>", line 2
print x
^
SyntaxError: invalid syntax
I'm in Windows 7, running R 3.1.0, with RStudio Version 0.98.847 (beta preview version). Interactive Python opens just fine from the command line.
Any ideas?
The easiest option is to uninstall python3 and install the most recent Python 2.7 (currently Python 2.7.6 ). Alternately, onward and upward -- use py3, which just involves possibly tweaking any existing examples you run into in knitr.
As noted on Wikipedia, Knitr is an engine for dynamic report generation with R, a statistics-oriented programming language. This article explains how to add R code to your LaTeX document to generate a dynamic output. In a standard LaTeX distribution you must have R set up in your operating system and run some special commands to compile it.
On recent versions of Windows, it is possible to run Python scripts by simply entering the name of the file containing the code at the command prompt: C:\devspace> hello.py Hello World!
Running Modules With the -m Option. Python offers a series of command-line options that you can use according to your needs. For example, if you want to run a Python module, you can use the command python -m <module-name>. The -m option searches sys.path for the module name and runs its content as __main__: $
Your issue is that you've installed python3, but the syntax you're using is python2. The py2 -> py3 transition involved changes to the language itself -- in your example, print
has changed from a syntax to a function. (So print(x)
would work in your code above.)
The easiest option is to uninstall python3 and install the most recent Python 2.7 (currently Python 2.7.6). Alternately, onward and upward -- use py3, which just involves possibly tweaking any existing examples you run into in knitr
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With