ipython from windows command line - ipython

I changed the environment variable PATH, to a new value and then back to what I believe was the original one. But now I can't open a .ipynb file through the Windows command line the was I was used to.
After changing the directory in command line and running
ipython notebook notebook_name.ipynb
I get the following message:
'ipython' is not recognized as an internal or external command.
My environment variable is set to a folder with python.exe, and this folder includes a subfolder with ipython.exe and jupyter-notebook.exe. When I open iPython command line and type %env, I can see the full path to the correct subfolder under PATH.
Can someone point to a solution?
Thanks.

So I figured out a solution - I changed the environment variable PATH to the subfolder with the .exe files. Although the path including this subfolder was listed under %env, it did not work without being referred directly in the System setting.

Looks like you figured out that you have to set the extension association in System Settings.
I've found that I'm using Notebooks often enough that it was well worth using AutoHotKey with the following script to open the Jupyter Notebook server in my default directory (or the currently highlighted directory in Explorer)
#SingleInstance Force
#NoTrayIcon
SetTitleMatchMode RegEx
; Press CTRL+ALT+I in a Windows Explorer window to launch a IPython notebook server in the current folder.
^+!i::
; Get the current path.
Send ^l
; Backup the current clipboard.
ClipSaved := ClipboardAll
; Copy and save the current path.
Send ^c
ClipWait
x = %Clipboard%
; Restore the clipboard.
Clipboard := ClipSaved
ClipSaved = ; Free the memory in case the clipboard was very large.
; Now, run the IPython notebook server.
RunWait, ipython notebook --notebook-dir "%x%", , min
return
^i::
; Now, run the IPython notebook server.
RunWait, jupyter notebook --notebook-dir "C:\Path\To\WorkSpace", , min
return
; Press CTRL+ALT+P to kill all Python processes.
^!p::
Run, taskkill /f /im python.exe, , min
return

Related

Matlab doesn't recognize user environmental variable

I installed an application named lqns in the path: /home/robb/Research/dist/lqns-6.2/lqns (lqns is a folder containing the executable lqns). I want the program to be executed in command line simply calling lqns in the shell, I solved this adding to the file ~/.bashrc the line:
export PATH=$PATH:/home/robb/Research/dist/lqns-6.2/lqns
And it works with no issue. I am now trying to execute this program inside a Matlab script, running:
[status, ~] = system("lqns " + filename, '-echo');
Where filename is the path of an input file. I get the error message:
/bin/bash: line 1: lqns: command not found
Running the exact same command with the shell I get no error: the program runs with no problem generating the relative output.
Running getenv('PATH'); in Matlab and printenv PATH on my OS shell I indeed get two different results: Matlab does't have the path to lqns. I even tried editing manually the files /etc/environment, /etc/bash.bashrc and /root/.bashrc, with no result. How can I solve this issue?
you need to launch matlab by typing matlab in a terminal, not by double clicking on its shortcut from your desktop. (or even typing ./matlab in a terminal from your desktop)
it's up to the operating system to determine what double clicking does, and it's not guaranteed to execute most of your shell initialization scripts (or even launch it from the correct shell to begin with).
more info at Why are environment variables not resolved when double-clicking .desktop file?

How do I add a keyboard shortcut to open VS Code on the folder from File Explorer?

I want to be able to press . in any folder that I'm in File Explorer and open Visual Studio Code on that folder. It is the same effect as right-clicking and clicking "Open with Code". Pressing . is just like I could do on the GitHub website.
It might not be officially possible, but are there any workarounds to make it work?
The biggest issue is getting the current location from windows explorer. This requires some reverse engineering to get the memory address where windows explorer stores the location. If for any reason this address changes, you will need to find it again.
Besides that, windows supports global keyboard hooks. So you'd like to set up a global keyboard hook for your . key. The windows API also provides methods to get the active window. So whenever the . key is pressed, you can check that the active window is an explorer window. Then you'd read the current folder from the explorer window's memory. After that you've got everyting you need to start visual studio code.
This is very hacky, but I guess it's the only choice you have. Topics to research are:
Keyloggers - These for instance use the global hooks I mentioned
Generic reverse engineering - To find the address of the current location in windows explorer
Edit: Autohotkey post about how to get the windows explorer location. If that works as people claim, autohotkey can also be used to register a hook for the . key and launch VS code.
Thank you #AdrAs and #SarvinR for the answers. I used Sarvin's solution for a while, while trying to google and make sense of Adr's solution. Sarvin's solution is very useful if you're not trying to download any external programs, but if you want the true solution to this question, I finally managed it here:
Download AutoHotKey. It's good if you're familiar with it. AHK basically creates hotkeys (or shortcuts) like Adr described.
(If you have an existing ahk that you use, you can skip these steps and copy the code block down below)
Create a new AutoHotKey script by right-clicking on your desktop or anywhere in file explorer (we're gonna move it later so it doesn't matter). Name it whatever you want. I'm going to call it MyScript.ahk for this answer (I actually used david.ahk for myself).
Now, open command prompt (win + r, cmd, enter) and look for where VSCode is by typing where code. It will probably give you two lines. Take note of one of the lines (I chose the top one).
Right-click the ahk script file that you just created and choose Edit Script (or you can open it with notepad++ or VSCode or any editor of your choice, it's just a normal text file). Delete everything and paste this in:
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
GetActiveExplorerPath()
{
explorerHwnd := WinActive("ahk_class CabinetWClass")
if (explorerHwnd)
{
for window in ComObjCreate("Shell.Application").Windows
{
if (window.hwnd==explorerHwnd)
{
return window.Document.Folder.Self.Path
}
}
}
}
#IfWinActive ahk_exe Explorer.exe
.::
path := GetActiveExplorerPath()
run, "C:\Users\david\AppData\Local\Programs\Microsoft VS Code\bin\code" "%path%"
return
On the second last line, replace the VSCode location with what you just saw in cmd. You most likely have to just change the username from david to your name.
Now, save the file try opening it (double click the ahk). If it works, a green H icon should appear on your tray without any errors. Go into any file directory in Windows File Explorer and hit . like you would normally do in GitHub. (Do Not do this in large directories like your root C:. There will be too many files for VSCode to load). It should work like expected, and if it doesn't, you did something wrong (I did the exact same thing like I just described and it works).
Now, of course, you would want to run this script on startup. Copy/Move the .ahk file into C:\Windows\System32. It will ask you for administrator permissions, so click yes. Open the registry editor (win + r, regedit, enter). Navigate to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run. On the right side pane, right click on the empty space then create a new String Value with any name (I used davidAHK) and set its value to your ahk file that you just copied/moved with quotes ("C:\Windows\System32\david.ahk" for my case). Close the Registry Editor and safely restart your computer. The ahk script should run on start up and you should be able to click . in any directory in file explorer to open VSCode.
Again, thank you #AdrAs and #SarvinR for your help!
I have an trick for this
Create vs.bat and write this code in that
#echo off
code %cd%
And Move this file to C:/Windows/System32/
Now All Set.
If you type vs on the address bar of explorer your vs code with current folder will open
open VSCode -> command+shift+p -> Enter shell command – > click the prompt "shell command: install 'code' command in path"
open directory -> code .
I wanted launch VS Code with the open folder OR the selected files, using AHK v2, and I wanted it to be plug & play for other users. Here's the result:
#Requires AutoHotkey v2.0
; This is the key combo.
#!.::
{
; Is the current window an Explorer window?
if (WinGetClass("A") == "CabinetWClass") {
; Cache the current clipboard contents.
clipboard := A_Clipboard
; Clear the clipboard & copy selected files.
A_Clipboard := ""
Send "^c"
ClipWait(0.5)
; If no files are selected...
if (A_Clipboard == "") {
; Get the current window's ID.
hwnd := WinGetID("A")
; Find the current window's COM object.
for window in ComObject("Shell.Application").Windows{
if (window && window.hwnd && window.hwnd == hwnd)
; Get the current folder's path.
path := window.Document.Folder.Self.Path
}
}
else {
; Quote & space-concatenate selected files.
path := '"' . StrReplace(A_Clipboard, "`n", '" "') . '"'
}
; Restore the clipboard.
A_Clipboard := clipboard
; Run code.
exe := '"' . StrReplace(A_AppData, "Roaming", "Local\Programs\Microsoft VS Code\code") . '"'
Run(exe . " " . path)
}
}
Works like a charm, and it's a piece of cake to install & run on your machine! Instructions at the repo.

How to open a tab/pane in the same directory in Windows Terminal with Fish shell

Microsoft has a guide how to do that for other shells such as bash and zsh but not for fish shell.
https://learn.microsoft.com/en-us/windows/terminal/tutorials/new-tab-same-directory
open your fish config file located at ~/.config/fish/config.fish and add to it the following function:
function storePathForWindowsTerminal --on-variable PWD
if test -n "$WT_SESSION"
printf "\e]9;9;%s\e\\" (wslpath -w "$PWD")
end
end
Explanation:
This function is a hook that is called whenever current path is changed.
It will confirm current session is opened by Windows Terminal (By verifying environment variable $WT_SESSION exists), and will send Operating System Command (OSC 9;9;), with the Windows equivalent path (wslpath -w) of current path.
This sequence will let Windows Terminal know that the path was changed and when a new tab / pane will be created, Windows Terminal will use the changed path as current directory.

How to exit Command Prompt after launching VSCode

Here are the steps to reproduce the problem:
Open Command Prompt. ( cmd )
Run code . to launching VSCode.
Type exit and hit Enter in the Command Prompt.
Then the Command Prompt is just paused. I have to wait VSCode exit to let Command Prompt window closed.
Does anyone know why? How can I close Command Prompt window without exiting VSCode?
I found a solution here: https://github.com/Microsoft/vscode/issues/6608. It involves changing the code.cmd file (usually found under "C:\Users\yourUsername\AppData\Local\Programs\Microsoft VS Code\bin").
Changing the fifth line in that file from
call "%~dp0..\Code.exe" "%~dp0..\resources\app\out\cli.js" %*
to
start "" "%~dp0..\Code.exe" "%~dp0..\resources\app\out\cli.js" %*
will make the cmd window close right away. But that change will apparently break some other things (specifically the --wait flag), so I figure it's wiser to leave the code.cmd file alone.
Instead I made a copy ("codeNoCommandPrompt.cmd") right beside it and changed the line in there. That works fine for my usecase, namely having VS Code start on a specific folder alongside a bunch of other programms via a script.
I just tested it (using the latest VSCode 1.24.1), and it does work: the CMD shell session closes immediately when typing "exit".
Try calling the code.cmd script with its full path to see if the issue persists:
"C:\Program Files\Microsoft VS Code\bin\code.cmd" .
Try also the same command after having simplified the PATH (for testing)
set PATH=C:\Program Files\Microsoft VS Code\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\
code .
Simply use this
code . && exit
Check your PATH variable, maybe you have duplicate entries for VS Code
Go System Properties -> Envieronment variables -> select PATH variable then click Edit.
Remove "C:**\Microsoft VS Code".
Leave intact "C:**\Microsoft VS Code\bin"
This happens when you reinstall vscode with "add to PATH" checked

Running a MATLAB script from Notepad++

Is there a way of running a MATLAB script from Notepad++?
Obviously I have MATLAB installed on my computer. I know you can set a path for Notepad++to run when you hit F5, but when I set this path to my MATLAB.exe file, it simply opens another instance of MATLAB.
This is not what I want, I want the actual script in Notepad++ to be executed in the already open and running instance of MATLAB.
I'm afraid I'm not on my home computer at the moment to test this out, so the following is just a suggestion for you to try.
If you take a look at the NppExec plugin for Notepad++, you'll see that with it you can specify a command to be run when you hit F6 (like an enhanced version of hitting F5 in the regular Notepad++). You can also give it variables such as the path to the current file, and the name of the current file.
MATLAB (on Windows at least - I assume you're on Windows) makes available an API over ActiveX/COM. If you search in the MATLAB documentation for details, it's under External Interfaces -> MATLAB COM Automation Server. By running (in MATLAB) the command enableservice('AutomationServer') you will set up your running instance of MATLAB to receive instructions over this API.
You should be able to write a small script (perhaps in VBScript or something similar) that will take as input arguments the path and filename of the current file in Notepad++, and will then connect to a running instance of MATLAB over the COM API and execute the file's contents.
Set this script to be executed in NppExec when you hit F6, and it should then run the current file in the open instance of MATLAB.
As I say, the above is just speculation as I can't test it out right now, but I think it should work. Good luck!
Use NppExec add-on and press F6, copy paste the following and save the script:
NPP_SAVE
set local MATPATH=C:\Program Files\MATLAB\R2015a\bin\matlab.exe
cd "$(CURRENT_DIRECTORY)"
"$(MATPATH)" -nodisplay -nosplash -nodesktop -r "try, run('$(FILE_NAME)'),
catch me, fprintf('%s / %s\n',me.identifier,me.message), end"
then run (press F6; enter). Matlab Console and Plot windows still open and stay open. Error messages will be displayed in opening Matlab command window. Adding
, exit"
to the last command will make it quit and close again. If you want to run an automated application with crontabs or the like, check Matlab external interface reference for automation.
matlab.exe -automation ...
Also works in cmd terminal, but you have to fill in the paths yourself.
This is a usable implementation upon Sam's idea. First, execute MATLAB in automation mode like this.
matlab.exe -automation
Next, compile and execute this following VB in NppExec plugin. (which is to use MATLAB automation API)
'open_matlab.vb
Imports System
Module open_matlab
' connect to a opened matlab session
Sub Main()
Dim h As Object
Dim res As String
Dim matcmd As String
h = GetObject(, "Matlab.Application")
Console.WriteLine("MATLAB & Notepad++")
Console.WriteLine(" ")
'mainLoop
while True
Console.Write(">> ")
matcmd = Console.ReadLine()
' How you exit this app
if matcmd.Equals("!!") then
Exit while
End if
res=h.Execute(matcmd)
Console.WriteLine(res)
End while
End Sub
End Module
Then you'll get a matlab-like terminal below your editor. You can then code above and execute below. type !! to exit the terminal.
What it looks like
Tips: don't use ctrl+c to interrupt the MATLAB command, because it will kill the whole process instead.