12

I'm currently writing some complex tests for our Django-Application in PyCharm. Therefore I'm using all those fancy colors on the console, which can be achieved by printing some special codes.

I'm now wondering whether it is also possible to print hyperlinks to specific code lines in the console. For every stacktrace PyCharm generates for all files such links, but is it possible to do that on my own?

That would be very helpful, to jump to the specific lines directly from the test output, instead of manually navigate to.

2
  • What are you trying to achieve through this? Commented Oct 11, 2014 at 8:21
  • 3
    I'm iterating over all my class-based views and execute some kind of automated tests for each. The progress ist printed on the console. If some view failes to pass the test, I want to click on a link in the console, which takes me directly to the view, so I can get faster to the failing code, instead of typing the view name in the search function. It's not a big problem, but this link would be more comfortable.
    – Gnietschow
    Commented Oct 11, 2014 at 11:40

3 Answers 3

8

I guess this is a little late and I am not 100% happy with it, but it just works (at least with PyCharm 2019.2). If you print out a line in the format of

File "{file_path}", line {line_number}

then PyCharm will automatically convert this line into a link to the corresponding file and line. For example:

print("File \"C:/Workspace/enventa-next/suites/suite_standard/tst_Inventur/test.py\", line 3")

Will jump to or open the file and move focus to the third line. I think a link with custom content is not possible.

4
  • 1
    For me (PyCharm 2020.2) it seems to have no effect.
    – CodePrinz
    Commented Sep 11, 2020 at 15:31
  • 1
    Works for me in 2020.2.3 Professional. Note: Using windows and backslash then you need to make sure to escape it with an additional backslash. (Better to just use forwardslash though, works as well.)
    – Mandera
    Commented Nov 21, 2020 at 6:46
  • "%(filename)s:%(lineno)d" works the same way. Now if I could only figure out why this prints as a link in my Pytest log and scratch file, but not in my class under test.
    – Noumenon
    Commented Apr 6, 2021 at 3:21
  • 1
    Only seems to work for *.py files, I wanted to add links to *.log files though.
    – xjcl
    Commented Mar 13, 2023 at 13:44
4

Function: print_link(file, line)

With the help of Frieder's answer I created print_link which allows you to easily create a link in your PyCharm console. It uses the built-in inspect module to create default values for file and line to point to the line that called it.

import inspect

def print_link(file=None, line=None):
    """ Print a link in PyCharm to a line in file.
        Defaults to line where this function was called. """
    if file is None:
        file = inspect.stack()[1].filename
    if line is None:
        line = inspect.stack()[1].lineno
    string = f'File "{file}", line {max(line, 1)}'.replace("\\", "/")
    print(string)
    return string
# Default to this line and file (randomtesting.py in generallibrary, line 14)
print_link()

# Link relatively to another repository through parent's directory
print_link("../generalvector/generalvector/vector.py", 23)

# Link relatively to README which is in same directory as randomtesting.py
print_link("README.md", 1)

# Link absolutely to any file
print_link("A:/hello.txt", 1)

print_link

Blue text means that the link is successful.

Function: print_link_to_obj(obj)

Building further on the previous function we can create print_link_to_obj to easily and dynamically create a link to any object which has it's source code available to inspect.

def print_link_to_obj(obj):
    """ Print a link in PyCharm to a module, function, class, method or property. """
    if isinstance(obj, property):
        obj = obj.fget
    file = inspect.getfile(obj)
    line = inspect.getsourcelines(obj)[1]
    print_link(file=file, line=line)
# Create a link to definition above
print_link_to_obj(print_link)

# Create a link to entire inspect module
print_link_to_obj(inspect)

# Create a link to a function in inspect
print_link_to_obj(inspect.stack)

# Create a link to a property in a class in inspect
print_link_to_obj(inspect.BoundArguments.args)

print_link_to_obj

Package: generallibrary PyPI pyversions

Install my cross-platform package if you don't want to copy-paste the functions:

pip install generallibrary
from generallibrary import print_link, print_link_to_obj
2

In PyCharm 2023.1.1 I am able to reference a specified line and even specified column.

print("file:///tmp/file%20with%20space.txt:18:3")

will print a hyperlink. Clicking on that will open a /tmp/filename with space.txt on line 18 column 3 in Kate editor (KDE).

3
  • This is the only solution that works 💪 Any idea how I can make the file to be opened in PyCharm directly without changing PyCharm to the default editor for .txt files?
    – themenace
    Commented Jun 13 at 11:05
  • Try this plugin: plugins.jetbrains.com/plugin/7677-awesome-console
    – Ashark
    Commented Jun 13 at 20:32
  • thx - but that doesn't seem to work :)
    – themenace
    Commented Jun 25 at 12:25

Not the answer you're looking for? Browse other questions tagged or ask your own question.