jupyter / help

:sparkles: Need some help or have some questions? Please visit our Discourse page.
https://discourse.jupyter.org
291 stars 97 forks source link

Running Unix command sin Jupyter notebook on Window 10 #181

Open shanedante opened 7 years ago

shanedante commented 7 years ago

gitbash I am working with Numpy and Pandas using Anaconda(Python) and Jupyter Notebooks on Windows 10.

I tried using Unix commands in Jupyter like !ls but the notebook says the command is not recognized. I was told I have to use Gitbash which I downloaded but I am unsure how to connect Gt bash to the notebook so the Unix commands will work.

I launch jupyter notebook with the jupyter command prompt and type (jupyter notebook) and it loads in a browser. However, I have no idea how to use the GitBash with the notebook. But when I type in ! ls in gitbash, I see all of the files I need to work with. Can someone please help me connect this? https://courses.edx.org/courses/course-v1:UCSanDiegoX+DSE200x+2T2017/discussion/forum prompt jupyter_command

/

takluyver commented 7 years ago

I don't think there's a good way to have it call git-bash on Windows. For ls specifically, we have a %ls magic which is an alias to the Windows dir command.

milindk commented 7 years ago

I was also facing the same issue, while going through those tutorials. However in place of ! as mentioned in the tutorial we need to place % and it works fine in windows 10.

So instead of !ls use %ls and click Shift+Enter, it displays the list of directories. Please see below snip.

Hope it helps

working_ls

takluyver commented 7 years ago

There are magic aliases for a few command commands like %ls, but not for all Unix commands.

cristovaov commented 7 years ago

I happen to stumble on this thread as I'm doing the same course as @shanedante. If you install Git Bash and check the box to add the UNIX tools to your PATH, you will be able to simply run the commands like !ls.

The problem I had was with sed and deleting empty lines as Jupyter calls CMD.exe - I wish there was a setting in Jupyter to set which shell to use :)

raavello commented 7 years ago

I'm just starting with pyttohn and I have the same problem. could it be realted with PowerShell? I installed git and the unix commands work well in powershell but no in CMD.

milindk commented 7 years ago

@raavello This is what we're trying to figure out.

cristovaov commented 7 years ago

Hi, for people taking the UCSD course: I think there's a lot of confusion on your ends. Also I didn't notice before but from the OP first screenshot, you don't do !ls in Git Bash itself - it's simply ls. The ! before a unix command is only in the Jupyter Notebook web interface.

Here's a step by step of my setup:

  1. Install Git Bash - at installation select the optionUse Git and the optional Unix tools from the Windows Command Prompt.
  2. Install Anaconda. Uncheck all boxes in the advanced installation screen - see this image.
  3. Launch Git Bash. It should open in your user directory (ex: C:\Users\Bob).
  4. In the Git Bash terminal activate the Anaconda environment with the command: source Anaconda3/Scripts/activate Anaconda3/
  5. cd into the folder where the course notebooks are. Example: cd Projects/dse200x/week-3/week-3-unix/
  6. Run jupyter notebook and your browser should open with Jupyter in the current directory.
  7. When finished, shutdown Jupyter in your terminal with ctrl+c and close the terminal window.

Like mentioned in my previous comment, you'll have to modify the sed command and the "deleting empty lines" bit as it behaves differently:

The problem I had was with sed and deleting empty lines as Jupyter calls CMD.exe

Good luck!

leandroamoras commented 6 years ago

@cristovaov thank you!

IvanOboth commented 6 years ago

Thanks @cristovaov !! You just saved the day!!

NielsHaw commented 6 years ago

When I open the Git Bash and type:

source Anaconda3/Scripts/activate Anaconda3/

I get: invalid Syntax.

Does anyone know what could be wrong?

pablohmoha commented 6 years ago

Thank you @cristovaov now Jupyter notebook runs

kpchowdary commented 6 years ago

The simple solution for the guys who ever getting errors while running Unix commands over jupyter notebook( through Anaconda3) is

-> Need to install git bash -> check current directory $pwd

c : \user\praveen

-> run jupyter notebook command

$ jupyter notebook

which will open note book and where we can run all Unix commands ex: !cat, !head, !ls....etc

ncoghlan commented 6 years ago

I actually did have the need to run git-bash from a notebook running in a Windows Python environment, and managed to get data out of it via the following arcane invocation:

import subprocess, pathlib, tempfile
GIT_BASH = r"C:\Users\NickCoghlan\AppData\Local\Programs\Git\git-bash.exe"
def run_git_bash_cmd(cmd):
    with tempfile.TemporaryDirectory() as tmpdir:
        target = pathlib.Path(tmpdir) / "output_file"
        dirname, fname = target.parts[-2:]
        sh_target = f"/tmp/{dirname}/{fname}"
        git_bash_cmd = [GIT_BASH, "--hide", "--no-needs-console", "-lc", f"{cmd} > {sh_target}"]
        print(git_bash_cmd)
        cmd_result = subprocess.run(git_bash_cmd)
        print(cmd_result)
        cmd_result.check_returncode()
        with target:
            result = target.open().read()
        return result

print(run_git_bash_cmd("env")) is then a handy way of checking whether or not the command is working as expected. (Note that my specific snippet runs a git-bash login shell, since I wanted ssh-agent to start automatically)