简体   繁体   English

如何在 Python 脚本中访问 Atom 工作区目录

[英]How to access Atom workspace directory in a Python script

I was working on a Python script, and wanted to access Atom's current working directory in that script.我正在研究 Python 脚本,并想在该脚本中访问 Atom 的当前工作目录。

Atom is built on electron.js, and the way you do it in JavaScript is: Atom 是基于 electron.js 构建的,您在 JavaScript 中执行此操作的方式是:

let filePath = atom.workspace.getActiveTextEditor().getPath();

I want this variable (simply the directory) in my Python script instead.我希望在我的 Python 脚本中使用这个变量(只是目录)。

The idea of what you're trying to achieve is giving me some headaches.你想要达到的目标让我有些头疼。 While it's certainly possible to invoke the Python interpreter from JavaScript, it's probably not the best user experience for people using your package.虽然当然可以从 JavaScript 调用 Python 解释器,但对于使用 package 的人来说,这可能不是最好的用户体验。

Before even running the Python script, the package needs to check at least the following:在运行 Python 脚本之前,package 至少需要检查以下内容:

  • user has the correct version of Python installed用户安装了正确版本的 Python
  • python is exposed to the PATH python暴露于PATH
  • the active file is eligible to be interpreted by Python活动文件有资格被 Python 解释

These are different problems, but nevertheless some you need to sort out.这些是不同的问题,但仍然需要解决一些问题。

You haven't specified, whether you invoke the Python script within a synchronous or asynchronous function.您尚未指定是在同步还是异步 function 中调用 Python 脚本。 I'm going to assume the former since it's easier to follow for JavaScript beginners, but you might want to change that in a later step我将假设前者,因为对于 JavaScript 初学者来说更容易理解,但您可能希望在稍后的步骤中更改它

JavaScript JavaScript

// Import Node's spawn method
const { spawnSync } = require('child_process');

// Get path of active file
const activeEditorPath = atom.workspace.getActiveTextEditor().getPath();

// Spawn child process
const child = spawnSync('python', ['path/to/your/script', activeEditorPath]);

Python Python

import sys

# Get argument
active_editor_path = sys.argv[1]

# Print editor path
print(f'Active editor path: {active_editor_path}')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM