question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow for setting sys.path in protostar.toml

See original GitHub issue

As mentioned in discord, I’d like to import my own python functions in hints. This is currently not straightforward because I have to manipulate sys.path.

What I’ve currently settled on is to do the path manipulation in __setup__ and imports in the hints themselves.

@external
func __setup__():
    %{
        import os, sys
        sys.path.append(os.path.abspath(os.path.dirname(".")))
    %}

    return ()
end

@external
func test_foo():
    # ...
    
    local expected
    %{
        from tests.my_module.utils import my_func
        ids.expected = my_func(42)
    }%
    # ...
end

It works, but could be made better.

One suggestion is to automatically have the directory in which the test_foo.cairo file is located placed in sys.path so that I can have utils.py sitting in the dir structure right next to my test files. Another thing that would be beneficial is to have a config setting in protostar.toml where I could list paths that I want to have added to sys.path for each test file so that I don’t have to manipulate the path inside __setup__.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Arcticaecommented, Sep 12, 2022

@milancermak you should be able to execute your original code in the newer protostar versions, since the root of the project is added to cairo-path. This might not work with additional CLI’s --cairo-path just yet, i’m going to start doing some work on that.

0reactions
Arcticaecommented, Sep 29, 2022

This should be possible right now, since pythonpath is being synced with cairo-path

Read more comments on GitHub >

github_iconTop Results From Across the Web

Testing | Protostar - Software Mansion
Protostar provides a flexible testing environment for Cairo smart contracts. It allows to write unit/integration tests with a help of cheatcodes.
Read more >
The initialization of the sys.path module search path — Python ...
A module search path is initialized when Python starts. This module search path may be accessed at sys.path. The first entry in the...
Read more >
Check and add the module search path with sys.path in Python
In Windows, you can add PYTHONPATH as well as normal environment variables by right-clicking on your PC (My Computer) -> System -> System...
Read more >
The Module Search Path - Real Python
In this video, you're going to explore the module search path. ... contained in the PYTHONPATH environment variable, if it is set.
Read more >
jedi.api.project — Jedi 0.18.2 documentation - Read the Docs
Projects can be saved to disk and loaded again, to allow project definitions to be used across repositories. """ import json from pathlib...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found