Technologicat / pyan

Static call graph generator. The official Python 3 version. Development repo.
GNU General Public License v2.0
324 stars 57 forks source link

Syntax error give by pyan #32

Closed abhigoku10 closed 3 years ago

abhigoku10 commented 4 years ago

@Technologicat hi thanks for working on pyan , currently i face issues for higher version of python3.6 which has different syntax as shown below

print(f"Kitti info train file is saved to {filename}") SyntaxError: invalid syntax

How to solve this ??

Technologicat commented 4 years ago

Hi,

This sounds like the version of Python, that Pyan happens to be running on in your example, doesn't support f-strings, so that Python is likely older than 3.6 (because f-strings were added in 3.6).

Pyan gets the AST from the same parser Python itself uses, and SyntaxError is only raised by that parser. (It is possible to raise a SyntaxError manually, since it's just an exception type like any other, but very few if any Python programs actually do that.)

So the issue you're seeing may be caused by the OS picking the wrong Python to run Pyan, if there are several Pythons installed. At least Linux systems commonly have more than one. For example, my Mint 19.1 installation has "2.7.15+" as its default Python, and I have to invoke python3 (or activate a virtualenv) to run Python 3.x.

If the OS is picking the wrong Python, that's my mistake, see issue #11. If I recall correctly, this isn't fixed yet in the pyan3 package on PyPI. (I've been away from Pyan for a while. In the meantime, some PRs have arrived for Pyan, so I'll handle those first before packaging the next release.)

When Pyan is running under Python 3.6 or later, your example should work. The AST node representing the f-string is not needed in the call graph analysis, so the analyzer automatically ignores it, and descends into child nodes, to analyze the parts inside a {...}.

This is done by fallback to the default generic_visit method of ast.NodeVisitor, which occurs because Pyan doesn't define the methods visit_JoinedStr or visit_FormattedValue. (In case you're curious about the node types, see GTS.) The default generic_visit method automatically descends into all child nodes.

So, as the first step, please check if Pyan is running under Python 3.6 or later? What is the output of python --version, and what is the first line in the pyan3 script on your system? To check the latter, cat $(which pyan3).

If the Python version command prints 2.something, then the easiest fix (in the meantime until the next Pyan release becomes available) is to change the first line in the pyan3 script to #!/usr/bin/env python3. (which pyan3 should show where the script is.) That change should make Pyan automatically use your default python3 (also in the currently active virtualenv, if you use venv or conda) when you run pyan3.

abhigoku10 commented 4 years ago

@Technologicat thanks for a quick response please find the following

1.So, as the first step, please check if Pyan is running under Python 3.6 or later? What is the output of python --version:::: --- python3.7 2.what is the first line in the pyan3 script on your system? To check the latter, cat $(which pyan3): i get that there is no file even though pyan3 file is there bash: cat/xyz/test/bin/pyan3: No such file or directory 3.If the Python version command prints 2.something, then the easiest fix (in the meantime until the next Pyan release becomes available) is to change the first line in the pyan3 script to #!/usr/bin/env python3-- no results

edumco commented 4 years ago

@abhigoku10 Can I help to reproduce your problem so we can find some solution?

pranjal-s commented 4 years ago

For me it works well within both conda's virtual environment with Python 3.7.7 and without virtual environment with Python 3.8 (had installed python3.8 as per https://tech.serhatteker.com/post/2019-12/how-to-install-python38-on-ubuntu/).

In either case, installed using:

python3 setup.py build
python3 setup.py sdist
python3 setup.py bdist_wheel --universal
python3 -m pip install .

After that I added following to my ~/.bashrc:

# Python Call Graph Vizualization
pygraph() {
    find . -iname "$1" | xargs pyan3 --dot --colored --no-defines --grouped | dot -Tpng -Granksep=1.5 -o pygraph.png
    display pygraph.png
}

Then, its simply pygraph test.py or pygraph '*.py' from anywhere.

@abhigoku10 Did you forget the 3 in python3 during running the above installation commands? Or maybe your virtual env (or system env) has Python 3.6 or lower and hence pyan3 can't interpret the Python 3.6+ files.

Technologicat commented 3 years ago

Cleaning up old issues.

PyPI package pyan3 updated. Please try again with the latest version, and reopen this issue if necessary.