crew102 / reprexpy

Render reproducible examples of Python code for posting to GitHub or Stack Overflow (port of R package reprex)

Home Page:https://reprexpy.readthedocs.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

IndentationError: unexpected indent

ncgoodbody opened this issue · comments

Hi! I came across this repository when I was looking for an equivalent of reprex for python. Thank you for putting this together!

I'm throwing an error when I try to run reprex. This is my test case:

In [1]: from reprexpy import reprex

In [2]: import pandas as pd

In [3]: df = read_csv('test_scores_01.csv')
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-3-55724d6bc285> in <module>()
----> 1 df = read_csv('test_scores_01.csv')

NameError: name 'read_csv' is not defined

And this is what happens when I run reprex.

In [4]: reprex(venue='so')
Rendering reprex...
Traceback (most recent call last):

  File "/Users/nicholascifuentes-goodbody/anaconda3/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2862, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)

  File "<ipython-input-4-2576ba93cef7>", line 1, in <module>
    reprex(venue='so')

  File "/Users/nicholascifuentes-goodbody/anaconda3/lib/python3.6/site-packages/reprexpy/reprex.py", line 323, in reprex
    statement_chunks = _get_statement_chunks(code_str, si=si)

  File "/Users/nicholascifuentes-goodbody/anaconda3/lib/python3.6/site-packages/reprexpy/reprex.py", line 20, in _get_statement_chunks
    tok = asttokens.ASTTokens(code_str, parse=True)

  File "/Users/nicholascifuentes-goodbody/anaconda3/lib/python3.6/site-packages/asttokens/asttokens.py", line 50, in __init__
    self._tree = ast.parse(source_text, filename) if parse else tree

  File "/Users/nicholascifuentes-goodbody/anaconda3/lib/python3.6/ast.py", line 35, in parse
    return compile(source, filename, mode, PyCF_ONLY_AST)

  File "<unknown>", line 3
    test_scores_01.csv
    ^
IndentationError: unexpected indent

Is there something I'm missing? Where is the indent error coming from?

When I run your example I get the following expected output:

from reprexpy import reprex
import pandas as pd
df = read_csv('test_scores_01.csv')
#> Traceback (most recent call last):
#> <ipython-input-5-29ed0ffc94ec> in <module>()
#> ----> 1 df = read_csv('test_scores_01.csv')
#> NameError: name 'read_csv' is not defined

I suspect what's happening in your case is that you have the output of a previously-rendered reprex on your clipboard instead of the code that you want to reprex. This happens to me sometimes - I copy some code to the clipboard that I want to reprex and then run reprex(). This puts the output of reprex() onto my clipboard. Then, when I go to re-runreprex(), I forget that my clipboard now has the output of the previous reprex call on it instead of the original input code.

You are totally right. 😣 Thank you for your help! This is an excellent project.