TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Show HN: Copilot-import – Import and run Python directly from GitHub Copilot

6 点作者 molenzwiebel将近 4 年前

1 comment

molenzwiebel将近 4 年前
A friend suggested we try do this after seeing stack-overflow-import (<a href="https:&#x2F;&#x2F;github.com&#x2F;drathier&#x2F;stack-overflow-import" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;drathier&#x2F;stack-overflow-import</a>), which queries stackoverflow for some matching result and loads it. For small functions, it&#x27;s surprisingly effective. You still need Copilot access to use this, unfortunately.<p>Some other examples:<p><pre><code> &gt;&gt;&gt; from copilot import rock_paper_scissors &gt;&gt;&gt; rock_paper_scissors(&#x27;Rock&#x27;, &#x27;Scissors&#x27;) &#x27;Player 1 wins&#x27; &gt;&gt;&gt; from copilot import get_current_git_commit &gt;&gt;&gt; get_current_git_commit() b&#x27;29c7e9b138a9247c598b613ca378d103de878e2a\n&#x27; &gt;&gt;&gt; from copilot import answer_to_everything &gt;&gt;&gt; answer_to_everything(1) 42 </code></pre> Practically, it works through two separate copilot calls. The first one asks copilot to predict the arguments a function would take, then the second one asks it to predict the body of the function. The returned code is then parsed, both to ensure that it is actual valid Python (it almost always is, although it sometimes spits out Python 2 or cuts off due to length limits), and to remove any extra statements that are not part of the function (sometimes it likes to include an `if __name__ == &quot;__main__&quot;` for example).<p>The function is then wrapped in an error handler that will attempt to include a module if the snippet references an undefined variable. Copilot tends to use the correct libraries, but forgets to import them so this step is necessary to get most things working. We experimented with prompting an `import ` inside the function body, which worked decent but delivered some wonky results when the code needed more than one import or when it needed none at all.