Spyder Shortcuts to find and open file? - python

I was an Emacs user and I used to find and open files with shortcuts Ctrl + x + f
Now my Python IDE is Spyder so I'm wondering if there is an equivalent command?
I have tried Ctrl + N and Ctrl + O but both returns a window which I need to use the mouse to select by clicking on it.

There is nothing as sophisticate as Ctrl+X+F in Spyder but you can use one of our IPython consoles along with our owm version of the %edit magic to navigate the file system like you'd do in a regular terminal and open files with it.
This is an example (in Linux)
In [1]: %edit /home/<Tab>
(where <Tab> means pressing the Tab key) will show you a widget with these completions (in my case)
carlos/
lost+found/
Then you can end up with %edit /home/carlos/foo.py and after hitting Enter foo.py will be opened in the Editor (if it exists, of course).
Additional Notes:
You can use the %cd magic to change your current working directory in an IPython console.
After you open Spyder, the initial cwd is the one shown in our working directory toolbar (usually placed above our Object Inspector).

I use the File Explorer window in Spyder, I find it really handy.

Related

VScode : Everytime I press Run python File in Terminal button, run in powershell always

Everytime I run python file at vscode(in this case I press Run python File in Terminal button), terminal is opened as Powershell always.
my select default profile settings are command prompt. so when I open a new terminal, it opens with cmd. However, when I run the python file even though the cmd terminal is open, a new powershell terminal is launched. and I don't know why...
How do i fix it ?
Yeah, it's an issue of VSCode In version v1.60.0. You can refer to GitHub issue #132150 for more details.
There are a lot of people report this kind of problem recently.
And the only workaround to this problem is to roll back the version to v1.59 until the release of v1.60.1 for now.
There might be an issue in your select default profile settings. However, you can always choose a default shell by pressing ctrl + ~ (~ tilda key) You can open up a shell in vscode, as seen below. By default this might open up powershell, from there you can select your default shell.
Note: Some of the options seen in the image require WSL2 (Windows Subsystem for Linux) to be installed and activated on any Windows system. Clicking any of those options will open a new tab of that shell, but if you click on the '+' plus button or restart vscode later, it will go back to the default shell (powershell) in our case.
Now click the option on the bottom that says select default profile
And you should see a window like this:
From there you can select the default shell in this window. And these changes will be saved, so anytime you click the '+' plus button to open a new shell instance, or relaunch vscode it will open your selected shell choice by default.
And that's it! From here you can select cmd since you want that to be your default shell. And you should be good to go. You can change these settings at anytime, by following these steps again.
Since you're using Python. I suggest using powershell for one reason. You can use a tool known as chocolatey which is like a package manager for windows that can download, install/update, and most importantly set environment variable paths in windows automatically. Which might be a lot easier than doing it manually on cmd.
To run the python script you can do the following:
$ python /path/to/file.py
If you want to run your python file in the terminal with cmd then you just open a new terminal and type the command
python "path/to/file.py"
If it doesn't work try using
py "path/to/file.py"

PyCharm focus Python Console for input upon run

In PyCharm 2018.2.5 (Community Edition), I have bound a keyboard shortcut to "Run File in Console". This launches opens the console and runs the file, but does not focus the console for input and anything I type goes into my code. (Sometimes when I use the switcher to focus the Python console it will try to search console variables when I type.)
Is there a way to automatically focus the Python console for input when it is launched?
No for PyCharm there is no way to automatically focus the Python console for input when it is launched.
In Notepad ++ i can do the following.
Enter my code
press shift+a to execute a macro which saves the file and execute it with Python 2.7 (or 3). I also made this macro a menu item.
When the console runs and the script asks for input you are already in focus and can directly put in our entry without having to click and highlight the window or so as it is necessary in PyCharm.
So the step between writing the code and executing it to see what it does exactly one hotkey. Its not one hotkey and a mouse click or what ever no.... its exactly one press of a button. This makes learning very effective because there are no detours.
I looked long in PyCHarm but i could not find this functionality or a workaround. So the answer to your question is "With PyCharm this is not possible."
Here is how to do it in Notepad ++.
Download Notepad ++
In N++ install the NppExec Plugin.
Create the "save and run in python" macro: In NppExec create a script as shown here:
NPP_CONSOLE 0
npp_save
npp_run cmd /K C:\Python27\python.exe "$(FULL_CURRENT_PATH)"
Save your macro script as say "Save and run in Python Selfmade"
In the hotykeys menu assign your macro to a new hotkey.
In the Npp_Exec menu click on Advanced Options and create a menu entry for your macro. To do this select the script you created under "Associated Script" and then click on "Add/Modify"
Another editor that offers you this quick and direct run functionality is "Atom" with its many plugins.
The shortcut to switch between the code and console is Alt+4; by clicking on the console or using this shortcut, future consoles you open will be selected by the input.
Yes it IS possible
in the run console change the view mode to Window (click the cog)
add the following lines to the benning of your source code
import pygetwindow as gw
win = gw.getWindowsWithTitle('Run')[0]
win.activate()
now it will automatically give focus to the run window... see my youtube demo.. https://www.youtube.com/watch?v=MhmltlUjU3k
i have just posted something related to this. It is needed to distinguish between "Python console" and "python or debug console" within the keymap options. The first one focus at the console but without automatically move caret to it, the later does it.
See more detailes in this answer.
It's very easy! Just make macros with two actions:
1 - Run command,
2 - Activate run tool window. And with keymap in settings create a shortcut. For example, I do this with shift + f10 .

cannot open python script by double clicking

i have searched on multiple forums and looks like others met similar issues but I haven't read a straightforward explanation yet. what I am trying to do is to simply open my python script by double clicking it. it used to work, but not anymore. my python scripts can still be opened via idle. and executable when it is opened and F5 pressed.
if on windows or linux, right click, open with ... select python executable, or Idle. whichever you want the default action to be.
then select the option to "always use this to open .py files"
If on a Mac, you can CTRL+Click the file, Get info > Open With and change it to Python Launcher. Click Change All. Double clicking should run the script.
Another way is to use pyinstaller which makes the script into an executable. It is pretty straightforward to use.
Change the "open with..." preference for the py files by shift + right click and choose open with from the menu. Make sure you tick the use always option

How can I run the current file in PyCharm

How can I run the current file in PyCharm? I would like a single hotkey that will execute the current file (whether normal file, scratch file, or scratch buffer) using the project default python interpreter. I would like to NOT have to create a custom run configuration, but just launch with the default Python configuration. Is such a thing possible?
As it turns out, the action I was seeking is "Run context configuration" (or "Debug context configuration" for debugging). The default key binding on Windows is ctrl+shift+f10, or ctrl+option+R on Mac, as Ev. Kounis pointed out, although you can bind it to any key you like.
These settings can be found under the "Other" section in File->Settings->Keymap. The easiest way to find them is to simply use the search box.
Just right click on the tab of your file and click run "file name" or you can press CTRL+shift+F10
The key combination you are looking for is Ctrl + Shift + F10. This will run the current script with current being the one displayed in the viewer.
There are several alternatives:
Run context configuration from editor shortcut:
Mac: ⌃⇧R
Windows/Linux: Ctrl + Shift + F10
You can right click on the file, and then run Run 'filename'.
You can also run the shortcut to Find Action and then type run:
Mac: ⇧⌘A
Windows/Linux: Ctrl + Shift + A
If the current file was just run, you can simply use the Run shortcut:
Mac: ⌃R
Windows/Linux: Shift + F10
References:
PyCharm Shorcuts for Mac OS X
PyCharm Shorcuts for Windows and Linux
Alt+Shift+F10 and then select the script you want to run.
After that Shift+F10 will run the last script that has been run.
Keyboard shortcuts can be different on some machines. So you can just click right key on mouse and then "Run "(also you can select part of code and do the same)
File->Settings->Keymap->Run->Run and see your current keymap
Basically, if you just need to run the current .py file in PyCharm. Right-click inside the file, and you can click the "Run file.py" button, and it also tells you the shortcut which on Mac is Control + Shift + R.
To Run/Execute current python file in Pycharm use following keys in windows machine
Shift+Ctrl+F10
OR
for selected code (specific line that are selected/marked)
Shift+Alt+E
Running file with Ctrl+Shift+F10 or via Run menu has some disadvantages. For example, you cannot use the custom redirect input file.
I have created a project it is that will actually allow you to use currently opened py file when you press run / debug in a standard way. My project will remain actual at least until jetbrains fix non working macros in configurations. See bug report.
As of PyCharm 2022.2 there's a built-in "Current File" configuration.
When intalling on Pycharm select :
Add launchers dir to the PATH: Allows running this PyCharm instance from the Console without specifying the path to it.
:-)

how do I launch IDLE, the development environment for Python, on Mac OS 10.7?

I am running python 2.7.1. I can't figure out how to launch the IDLE IDE. I am told it comes already installed with python, but I can't find it using spotlight.
In the stock Mac OS X python installation, idle is found in /usr/bin, which is not (easily) accessible from Finder and not indexed by Spotlight. The quickest option is to open the Terminal utility and type 'idle' at the prompt. For a more Mac-like way of opening it, you'll have to create a small app or shortcut to launch /usr/bin/idle for you (an exercise left to the reader).
When you open up a new terminal window, just type in
idle
Then you will see a little rocket icon show up as IDLE loads
Then the Python shell opens up for you to edit
I think the shell command is
python -m idlelib.idle
but i am not a mac user so i can't test.
One way to run IDLE from spotlight or an icon in the Applications folder is to build a quick Automation for it. As mentioned by other commentators, this probably isn't necessary for Python 3, as it creates a shortcut automatically, and some hand-installed versions have tools to do this automatically. But if you want to roll your own:
You'll need to know the terminal command to open your version of IDLE. On my Mac right now (early 2016), running python 2.7.10, it is "idle2.7"
Using spotlight, or in the Utilities folder, open "Automator"
Choose an "Application" type document.
Make sure "Actions" is selected in the gray bar, upper left.
In the actions column, find "Run Shell Script" and double-click it, or drag it to the workflow area on the right.
Enter the terminal command in the parameters box that appears.
Save your automation (I called mine "IDLE" and put it in the Applications folder, to make it easy).
It's now available (as soon as spotlight indexes it) via all the normal methods. The only side-effect will be that while it's running, your menu bar will have a spinning gear over in the tray area next to the clock. This indicates an automation workflow is running. Once you close IDLE, it will go away.
first to launch the terminal CMD+space
second to input idle3
the idle will be activated automatically.
After you launch idle from the command line (make sure idle shell window has focus), click File, click "New File". A new window opens; this is your editor.
Type your program in the editor. Click "File", click "Save As...". Save your file somewhere with any name you choose, and a ".py" extension to the file name.
Click "Run", click "Run Module" (or, F5). Assuming no errors, the results will appear in the Shell window. Edit your file & repeat as necessary.
The answer of Matthewm1970 works like a charm!
And if you add an & to your shell command, the automation script will end immediately. There is no spinning gear. Like so:
/usr/local/bin/idle3.5&
Note the ampersand.
Cheers.
-melle
open Terminal
type "idle" and press enter
right click on "Python" icon > Options > Keep in Dock
so for python 3.4.3 in applications a folder named "python 3.4" click that and click IDLE.
for python 2.7.9 go here https://www.python.org/downloads/ and get 2.7.9 and a folder named "python 2.7" click that and click IDLE.
As to the earlier questions about starting IDLE: you can certainly start it from the command line. Also, if you installed Python using Homebrew, you can run 'brew linkapps' (from the command line); that will place an app for IDLE (among other things) in Launchpad (Applications folder).

Categories

Resources