Python 从命令行(终端)启动 Pycharm

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22133861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:19:51  来源:igfitidea点击:

Launch Pycharm from command line (terminal)

pythoncommand-lineenvironment-variablespycharmsage

提问by torrho

I want to try out PyCharm for sage mathematics development. Normally I run eclipse to do sage development, but now I want to try it with PyCharm.

我想尝试使用 PyCharm 进行 sage 数学开发。通常我运行 eclipse 来做 sage 开发,但现在我想用 PyCharm 尝试它。

To launch eclipse with sage environment variables, in command line I normally do the following:

要使用 sage 环境变量启动 eclipse,我通常在命令行中执行以下操作:

sage -sh
cd /path/to/eclipse
./eclipse

The first line loads the sage environment variables, the remainder launches eclipse. How can I do the same thing for pyCharm? (note I am using a Mac and Ubuntu for sage development; the commands above are agnostic to both OSes)

第一行加载 sage 环境变量,其余行启动 eclipse。我怎样才能为 pyCharm 做同样的事情?(请注意,我使用 Mac 和 Ubuntu 进行 sage 开发;上面的命令对两种操作系统都不可知)

  1. Link 1is close to the solution I am looking for, however I cannot find a pyCharm.sh anywhere.
  2. Link 2: Jetbrains does not give clear instructions either.
  1. 链接 1接近我正在寻找的解决方案,但是我在任何地方都找不到 pyCharm.sh。
  2. 链接 2:Jetbrains 也没有给出明确的说明。

采纳答案by joest

Edit (April 2020): It seems that launcher script creation is now managed in Toolbox App settings. See the Toolbox App announcementfor more details.

编辑(2020 年 4 月):现在似乎在 Toolbox App 设置中管理启动器脚本的创建。有关更多详细信息,请参阅Toolbox 应用程序公告

--

——

  1. Open Application Pycharm
  2. Find tools in menu bar
  3. Click Create Command-line Launcher
  4. Checking the launcher executable file which has been created in /usr/local/bin/charm
  5. Open project or file just type $ charm YOUR_FOLDER_OR_FILE
  1. 开放应用 Pycharm
  2. 在菜单栏中查找工具
  3. 点击 Create Command-line Launcher
  4. 检查已创建的启动器可执行文件 /usr/local/bin/charm
  5. 打开项目或文件只需键入 $ charm YOUR_FOLDER_OR_FILE

Maybe this is what you need.

也许这就是你所需要的。

回答by Amit Verma

You can launch Pycharmfrom Mac terminal using the open command. Just type open /path/to/App

您可以Pycharm使用 open 命令从 Mac 终端启动。只需输入open /path/to/App

Applications$ ls -lrt PyCharm\ CE.app/
total 8
drwxr-xr-x@ 71 amit  admin  2414 Sep 24 11:08 lib
drwxr-xr-x@  4 amit  admin   136 Sep 24 11:08 help
drwxr-xr-x@ 12 amit  admin   408 Sep 24 11:08 plugins
drwxr-xr-x@ 29 amit  admin   986 Sep 24 11:08 license
drwxr-xr-x@  4 amit  admin   136 Sep 24 11:08 skeletons
-rw-r--r--@  1 amit  admin    10 Sep 24 11:08 build.txt
drwxr-xr-x@  6 amit  admin   204 Sep 24 11:12 Contents
drwxr-xr-x@ 14 amit  admin   476 Sep 24 11:12 bin
drwxr-xr-x@ 31 amit  admin  1054 Sep 25 21:43 helpers
/Applications$
/Applications$ open PyCharm\ CE.app/

回答by ford

You're right that the JetBrains help pageisn't very clear. On OS X, you'll want to use the launcher at:

您说得对,JetBrains 帮助页面不是很清楚。在 OS X 上,您需要在以下位置使用启动器:

/Applications/PyCharm.app/Contents/MacOS/pycharm

Or, for community edition:

或者,对于社区版:

/Applications/PyCharm\ CE.app/Contents/MacOS/pycharm

Unfortunately, adding a symlink to this binary wouldn't work for me (the launcher would crash). Setting an alias worked, though. Add this in your .bash_profile(or whatever shell you use):

不幸的是,向这个二进制文件添加符号链接对我不起作用(启动器会崩溃)。不过,设置别名有效。将此添加到您的.bash_profile(或您使用的任何外壳)中:

alias pycharm="/Applications/PyCharm CE.app/Contents/MacOS/pycharm"

Then, you can run commands with simply pycharm.

然后,您可以使用简单的pycharm.

With this you can do things like open a project:

有了这个,你可以做一些事情,比如打开一个项目:

pycharm ~/repos/my-project

Or open a specific line of a file in a project:

或者在项目中打开文件的特定行:

pycharm ~/repos/my-project --line 42 ~/repos/my-project/script.py

Or view the diff of two files (they don't need to be part of a project):

或者查看两个文件的差异(它们不需要是项目的一部分):

pycharm ~/some_file.txt ~/Downloads/some_other_file.txt

Note that I needed to pass absolute paths to those files or PyCharm couldn't find them..

请注意,我需要将绝对路径传递给这些文件,否则 PyCharm 找不到它们。

回答by Speedy99

I normally alias using built-in application launcher (open) from OS X:

我通常使用 OS X 的内置应用程序启动器(打开)别名:

alias pc='open -a /Applications/PyCharm\ CE.app'

Then I can type:

然后我可以输入:

pc myfile1.txt myfiles*.py

Though you can't (easily) pass args to PyCharm, if you want a quick way to open files (without needing to use full pathnames to the file), this does the trick.

虽然您不能(轻松)将 args 传递给 PyCharm,但如果您想要一种快速打开文件的方法(无需使用文件的完整路径名),这可以解决问题。

回答by rjurney

The included utility that installs to /usr/local/bin/charmdid not work for me on OS X, so I hacked together this utility instead. It actually works!

安装到的包含的实用程序/usr/local/bin/charm在 OS X 上对我不起作用,所以我改用了这个实用程序。它确实有效!

#!/usr/bin/env bash

if [ -z "" ]
then
  echo ""
  echo "Usage: charm <filename>"
  exit
fi
FILENAME=

function myreadlink() {
  (
  cd $(dirname )         # or  cd ${1%/*}
  echo $PWD/$(basename ) # or  echo $PWD/${1##*/}
  )
}

FULL_FILE=`myreadlink $FILENAME`;

/Applications/PyCharm\ CE.app/Contents/MacOS/pycharm $FULL_FILE

回答by Ami Mahloof

Use Tools -> Create Command-line Launcher which will install a python script where you can just launch the current working folder using charm .

使用 Tools -> Create Command-line Launcher 这将安装一个 python 脚本,您可以在其中使用 char 启动当前工作文件夹。

Very important!

很重要!

Anytime you upgrade your pyCharm you have to re-create that command line tool since its just a python script that points to a pyCharm configuration which might be outdated and will cause it to fail when you attempt to run charm .

每当您升级 pyCharm 时,您都必须重新创建该命令行工具,因为它只是一个指向 pyCharm 配置的 python 脚本,该配置可能已过时并在您尝试运行时导致它失败 charm .

回答by augustocbx

Inside the IDE, you can click in:

在 IDE 中,您可以单击:

Tools/Create Command-line Launcher...

工具/创建命令行启动器...

Create Command-line Launcher

创建命令行启动器

回答by Blaise

Update: My answer no longer works as of PyCharm 2018.X

更新:从 PyCharm 2018.X 开始,我的答案不再有效

On MacOS, I have this alias in my bashrc:

在 MacOS 上,我的 bashrc 中有这个别名:

alias pycharm="open -a /Applications/PyCharm*.app"

I can use it like this: pycharm <project dir or file>

我可以这样使用它: pycharm <project dir or file>

The advantage of launching PyCharm this way is that you can open the current dir in PyCharm using pycharm .(unlike /Applications/PyCharm*.app/Contents/MacOS/pycharm .which opens the PyCharm application dir instead)

以这种方式启动 PyCharm 的优点是您可以使用pycharm .(与/Applications/PyCharm*.app/Contents/MacOS/pycharm .打开 PyCharm 应用程序目录不同)在 PyCharm 中打开当前目录



Update: I switched to JetBrains Toolbox to install PyCharm. Finding PyCharm has gotten a bit more complex, but so far I was lucky with this monster:

更新:我切换到 JetBrains Toolbox 来安装 PyCharm。查找 PyCharm 变得有点复杂,但到目前为止,我很幸运遇到了这个怪物:

alias pycharm="open -a \"$(ls -r  /Applications/apps/PyCharm*/*/*/PyCharm*.app | head -n 1 | sed 's/:$//')\""

回答by Nikhil Rawat

To open PyCharm from the terminal in Ubuntu 16.04, cd into

要在 Ubuntu 16.04 中从终端打开 PyCharm,请 cd 进入

{installation home}/bin

which in my case was

在我的情况下是

/home/nikhil/pycharm-community-2018.1.1/bin/

and then type:

然后输入:

./pycharm.sh

回答by EL_DON

After installing on kubuntu, I found that my pycharm script in ~/bin/pycharmwas just a desktop entry:

在kubuntu上安装后,我发现我的pycharm脚本里面~/bin/pycharm只是一个桌面入口:

[Desktop Entry]                                                                                                                                                                                                 
Version=1.0
Type=Application
Name=PyCharm Community Edition
Icon=/snap/pycharm-community/79/bin/pycharm.png
Exec=env BAMF_DESKTOP_FILE_HINT=/var/lib/snapd/desktop/applications/pycharm-community_pycharm-community.desktop /snap/bin/pycharm-community %f
Comment=Python IDE for Professional Developers
Categories=Development;IDE;
Terminal=false
StartupWMClass=jetbrains-pycharm-ce

Obviously, I could not use this to open anything from the command line:

显然,我无法使用它从命令行打开任何东西:

$ pycharm setup.py
/home/eldond/bin/pycharm_old: line 1: [Desktop: command not found
/home/eldond/bin/pycharm_old: line 4: Community: command not found

But there's a hint in the desktop entry file. Looking in /snap/pycharm-community/, I found /snap/pycharm-community/current/bin/pycharm.sh. I removed ~/bin/pycharm(actually renamed it to have a backup) and then did

但是桌面入口文件中有一个提示。往里看/snap/pycharm-community/,我发现/snap/pycharm-community/current/bin/pycharm.sh。我删除了~/bin/pycharm(实际上将它重命名为有备份)然后做了

ln -s /snap/pycharm-community/current/bin/pycharm.sh pycharm

where again, I found the start of the path by inspecting the desktop entry script I had to start with.

再次,我通过检查我必须开始的桌面条目脚本找到了路径的开始。

Now I can open files with pycharm from the command line. I don't know what I messed up during install this time; the last two times I've done fresh installs, it's had no trouble.

现在我可以从命令行使用 pycharm 打开文件。我不知道这次安装过程中我搞砸了什么;最近两次我进行了全新安装,没有任何问题。