Executing Windows PowerShell commands in a Jupyter Notebook - python

Would someone please suggest a way to execute Windows PowerShell commands (i.e., commands more similar to Unix commands than Command-Prompt commands) in a Jupyter notebook? Please note that I am a layperson.
So far, I have added "C:\ProgramData\Anaconda3\Scripts" to my path system variable, which allows me to run "jupyter-notebook" from either Command Prompt or Windows PowerShell. I guess a shell looks in all folders in my path system variable for an application to run named "juypter-notebook". I guessed that running Jupyter Notebook from Windows PowerShell would allow my Jupyter-Notebook Chrome application to understand PowerShell commands, but this doesn't seem to be the case.

You can execute a PowerShell command from a Jupyter Notebook cell on a Windows machine with:
!Powershell.exe -Command "<PowerShell command>"
For example, if you wanted to print the first 10 lines of the file filename.txt, you could write:
!Powershell.exe -Command "type filename.txt -Head 10"
Source

Related

How to set cwd in VSCode Ipython?

I'm migrating from Spyder to VSCode and would like to use it in a similar way.
One of the major problems I'm facing is to set de cwd to the folder of the file I'm working. There is a lot of question just like mine in the Google, but none of them solved my problem.
Here are my configs:
"code-runner.cwd": "${fileDirname}",
"code-runner.fileDirectoryAsCwd": true,
"terminal.integrated.cwd": "${fileDirname}",
"python.testing.cwd": "${fileDirname}",
"python.terminal.launchArgs": [
"-m",
"IPython"
    ],
"terminal.integrated.defaultProfile.windows": "Command Prompt",
"python.terminal.executeInFileDir": false
What I want to do is to execute my file in IPython with the cwd setted to my file directory, to load some CSVs that are in the file folder, also be able to run some independent lines or enter some code directly in Ipython with all the variables loaded.
I don't want to just RUN the file or DEBUG, because I want to prototype some lines of code right in the IPython console with all the variables loaded.
With my configs, what I usually do is to select all the lines of the script and CTRL + ENTER. This will open a terminal with Ipython but setted to my project folder, not my file folder.
If I open a REPL, I get the same problem: Ipython opened but not set to file folder. Worse I can't use the CTRL + Enter to send some independent lines to Ipython (in this situation CTRL + Enter will cause to open a new terminal)
If I open a terminal by the Terminal menu, the console is setted to my file folder, but without python.
The closest that I got is to open the terminal from Terminal menu, and in the terminal navigate to my python and execute IPython, but then I can't run some independent lines from my code using a shortcut (most precisely like the F9 in Spyder)
My Python is not in Windows path. I'm working with files from a disk E: and my python is on a disk C:.
No Work Reason:
"terminal.integrated.cwd": "${fileDirname}", is the configuration of the default terminal. You can find out the terminal name was powershell or something others. But when you take the command of Run Selection/Line in Python Terminal, the name of the terminal was the explicit name: Python.
Impossible Reason:
Unfortunately, the Python extension seems has not provided the configuration to modify the cwd when you take the command of Run Selection/Line in Python Terminal while you can modify the cwd when you take the command of Run Python File in Terminal. As the IPython has no configuration to modify the cwd too, so it seems has not a suitable method to achieve your aim.
Workaround:
Method1: After you into the REPL mode, modify the cwd like this:
import os
os.chdir('/tmp')
Method2:
Change the configuration: "python.terminal.executeInFileDir": true
Execute the command of Run Python File in Terminal first, in order to change the terminal path.
Execute the command of Run Selection/Line in Python Terminal.
Method3:
Execute the command of Run Selection/Line in Python Terminal.
Exit out and change the path of the terminal.
Run IPython again.

Windows Jenkins "Windows Batch Command" vs Command Prompt

We are using jenkins to run a python script through command prompt. The command is like C:\Program Files\...\someapp.bat test.py. The someapp.bat is a customized python and it has a library called arcpy. The test.py file includes a statement import arcpy.
This command works fine on command prompt on the machine. However, when running through Jenkins, it complains arcpy does not exist. Then I used ssh to access the machine and ran the same command. It has the same error. Then I compared the path in the two command prompts (on machine and ssh), the environments has only one difference which is the Windows. One under user folder and the other under system. But it does not seem to be a dependency of the python bat.
So, in general, what's difference between a remote command line window and a local command line window if logged in as the same user? Any clue is helpful!

Visual Studio Codes cmd working different to external cmd

Error (Translated): The command "ffplay" is either misspelled or
could not be found.
I have the newest version of vsc installed and the path for the cmd is set to the original path "C:\WINDOWS\system32\cmd.exe". Both terminals have the same path.
Typing in 'python' works on both terminals, while plenty of other commands only work in the external cmd.
Why am i getting this error?
This problem also prevents me from using the module subprocess as every command is just 'not found'.
I noticed that your terminal window has a specific title, which means that you are not using the system default terminal. If you want to use the same environment variable in vscode, a common practice is to start vscode from that terminal, and the vscode that starts this way inherits its environment variable. Enter "code" in the terminal prompt and return to start vscode

What is the difference between a Python terminal vs. a bash terminal in VS Code?

I'm often in VS Code writing a .py file, with a python environment chosen. The integrated bash terminal is enabled by default. In the command window, I have the option to choose "Python: create terminal". The terminals seems the same to me. What are the differences?
More info: The terminal is not interactive at all. It looks and acts just like the bash terminal from what I can tell. Typing a Python command like "import re" results in the same message as a bash terminal in vs code (picture below)
There is at least one difference. Starting a new bash terminal in VS Code will open with your system's existing python environment as active (the one seen in a new terminal outside VS Code). Creating a terminal with the "Python: create terminal" command in VS Code opens an integrated terminal with the same active environment used by the interpreter.
For instance, if you are in this state with m5 as the interpreter environment in VSCode:
# conda environments:
#
base * /home/jpm/miniconda3
m5 /home/jpm/miniconda3/envs/m5
VsCode will open a regular bash shell with 'base' active . VsCode will open a Python shell with 'm5' active. Behavior with venv and conda environments is similar.
If you mean clicking right click and run selection/line in python terminal,
what that does is writes the command 'python' in terminal like you would, and it becomes a python command line, where you can type python commands that you would write in a script file. So you can write import {package} and it would import it. As opposed to the bash terminal, which is just a normal terminal embedded in VSCode

Anaconda Prompt Instead of PS in Microsoft Visual Code

Just installed Anaconda3. It came with the Microsoft Visual Code editor. I would like to use the Anaconda Prompt instead of Powershell or the CMD window from within the editor. Is there a way to change the default?
I didn't find the exact answer I was looking for but I believe my solution may help others so I'm posting my workaround:
Conveniently the Windows CMD or Powershell terminal opens in the same directory as the folder loaded into the Visual Code editor so I wanted to maintain this functionality. Here is how I did it without changing any Environmental Variables.
First change the default terminal from within Visual Code to the CMD terminal instead of Powershell. Add the following code to a batch file.
call "c:\anaconda3\Scripts\activate"
Then I named the batch file and saved it to my root directory. In my case snake.bat. Now when I launch my CMD terminal I just type c:\snake.batand the CMD prompt changes into an Anaconda prompt.
The Visual Code Editor attempts to do this already, but has errors. I believe this is because it opens with Powershell instead of the CMD terminal by default.

Categories

Resources