Related posts: Open an ipython notebook via double-click on osx
How can I open Ipython notebook from double click? I always cd to the directory of the notebook, then type ipython notebook in the browser and then open it in the browser. This steps is very inconvenient.
In windows, I remembered that I can change the directory in the browser, but in linux there is no way to explore to other directory via browser, if I want to open a new book in another directory, I have to restart another kernel as above, which annoys me.
Is there any simple and verified way to do this?
You can use a project like nbopen that handle that and will open the browser on the right notebook + start an IPython server if one is not yet running.
pip install nbopen.
open Automator, create new Application
Drag'n drop Run Shell Script
Change Pass input to as arguments
Copy/paste this script:
variable="'$1'"
the_script='tell application "terminal" to do script "nbopen '
osascript -e "${the_script}${variable}\""
Save the new application to Applications directory as nb_open
Right click any ipynb file and select "Open with > Other" and select the nb_open in the Applications folder. Don't forget to check "Always Open With".
Select an ipynb file, get info (command + i) > Open With (select nb_open if not selected already) > Click Change All.... Done.
To "promote" Yogesh's helpful comment to a fully self-contained answer:
Windows 10
In a CMD or PowerShell window with administrative rights (e.g. launched quickly with Win+X, then A), run:
pip install nbopen
python -m nbopen.install_win
Profit!
Double-click on *.ipynb files now starts a new server or reuses an existing instance.
One way to open a Jupyter notebook directly by double-clicking on the file is to associate the .ipynb file extension with the jupyter-notebook command. Here's how to do it on a Windows system:
Right-click on the Jupyter notebook file that you want to open.
Select "Open with" and then choose "Choose another app".
Select "More apps" and then scroll down to the bottom and choose "Look for another app on this PC".
Navigate to the directory where the jupyter-notebook.exe file is located (usually in the Scripts subdirectory of your Python installation), and select it.
Check the box next to "Always use this app to open .ipynb files" and then click on "OK".
Now, when you double-click on an IPython notebook file, it should open directly in the Jupyter notebook.
On a Mac or Linux system, you can set the default application for .ipynb files by using the xdg-mime command. First, determine the full path to the jupyter-notebook executable:
which jupyter-notebook
This will return the path to the executable. Then, use the xdg-mime command to set the default application for .ipynb files:
xdg-mime default jupyter-notebook.desktop application/x-ipynb+json
Replace jupyter-notebook.desktop with the path to the jupyter-notebook executable that you determined earlier. Now, when you double-click on an IPython notebook file, it should open directly in the Jupyter notebook.
Related
I'm setting up my home computer for basic python development. I've managed to install Anaconda through the command line but I'm not sure how to make .py files run through the Anaconda interpreter when double-clicked.
i.e. I did this on my Windows computer at work by right clicking on a .py file and then choosing open with > open with another application > navigating to {C:\Users\\AppData\Local\Continuum\anaconda3} > clicking on python.exe and then clicking 'open'. Is there an equivalent way of doing this on Linux? I'm running Pop!_OS.
I've tried clicking 'open with' but there was no option to open with another application. I also wouldn't know what type of file I would be looking for whereas I knew in Windows I was looking for a .exe
I've also tried right clicking on a .py file, going on the 'Permissions' tab, checking the box for 'Allow executing file as a program' and going to Files > preferences > Behaviour and changed the Executable Text Files option from 'Display them' to 'Run them'.
The most common way on linux to make things executable is by adding the executable flag to the file. On command line, if your file is called script.py:
chmod +x script.py
It is important that your script starts with a description of how to execute it.
For python, your script should start with:
#!/usr/bin/env python
Also, for clarification, there is no .exe file in Linux. The equivalent to that is the previously mentioned executable flag.
I want to change the default directory that Jupyter Notebook opens on. My OS is windows 10. I changed the c.NotebookApp.notebook_dir setting in the jupyter_notebook_config.py file. This works when I open Jupyter Notebooks thru Anaconda navigator, i.e. if I open Anaconda Navigator first and then launch Jupyter Notebooks from there.
MY problem is that when I launch jupyter notebook by typing it in the windows start/search menu, it does not open in the directory specified in the c.NotebookApp.notebook_dir setting in the jupyter_notebook_config.py file.
Any help on this will be appreciated. Thanks in advance!
When you typing it in windows search box, you run the shortcut actually, even you had edit c.NotebookApp.notebook_dir setting, just right click the the shortcut and open the files location or edit the shortcut directly
ex: C:\Users\user\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Anaconda3 (64-bit)\Jupyter Notebook.lnk,
right click the properties of shortcut, you will see the target is
C:\Users\user\Anaconda3\Scripts\jupyter-notebook-script.py "C:\Users\user",
delete the default value to
C:\Users\user\Anaconda3\Scripts\jupyter-notebook-script.py
and run it, it will get the c.NotebookApp.notebook_dir setting
I'm new to coding but I simply want to change directory and run jupyter. The problem is cmd instantly closes once it reaches the jupyter notebook command. Tried cmd /k too but it doesn't have an effect. I must being doing this wrong.
F:
cd directoryname
activate environmentname
jupyter notebook
pause
Solution:
The commands were closing the prompt for some reason when executed in a .bat (they don't when typed). The fix was to type call before the commands.
F:
cd directoryname
call activate environmentname
call jupyter notebook
pause
The commands were closing the prompt for some reason when executed in a .bat (they don't when typed). The fix was to type call before the commands.
F:
cd directoryname
call activate environmentname
call jupyter notebook
pause
Create a simple batch file (jnote.bat):
#echo off
call jupyter notebook "%CD%"
pause
In the same folder create a shortcut to the batch file and rename it to jupyter-notebook.
Open the shortcut properties and change the icon to the jupyter.ico. You will find this in the .\Menu sub-folder in your Anaconda distribution. You should now have a shortcut with the) nice jupyter icon.
Copy the shortcut to all the folders that you use for your notebooks.
Double-click the shortcut to open jupyter-notebook that folder.
For Jupyter, use:
%USERPROFILE%\Anaconda3\python.exe %USERPROFILE%\Anaconda3\cwp.py %USERPROFILE%\Anaconda3 %USERPROFILE%\Anaconda3\python.exe %USERPROFILE%\Anaconda3\Scripts\jupyter-notebook-script.py "**file location**"
for Jupyter lab:
%USERPROFILE%\Anaconda3\python.exe %USERPROFILE%\Anaconda3\cwp.py %USERPROFILE%\Anaconda3 %USERPROFILE%\Anaconda3\python.exe %USERPROFILE%\Anaconda3\Scripts\jupyter-lab-script.py %USERPROFILE%
Save it in a .bat file, with necessary changes at 'UserName' and 'File Location' . Keep it as a single line. Just double click the file to open jupyter notebook at the location.
Note: File Location is the location of the notebook to open.
Alternatively, you can use the same commands to create windows shortcut, without the need for the .bat file:
On the desktop, right-click -> new -> shortcut.
As the location of the item, paste the code above.
Then, you can double click the shortcut to launch jupyter. Unlike the bat file, you can also pin this shortcut to taskbar.
For details on how to setup jupyter lab, check out Running JupyterLab as a Desktop Application in Windows 10
Assuming activate and jupyter are executables or otherwise valid commands, everything should be okay. Since you're saying the cd command is probably the culprit, try the below:
Perhaps you are trying to change to a directory on a different drive... if this is the case you will need to use cd /d directoryname instead.
If this doesn't work, try putting a bunch of pause statements between each command to see exactly where it is breaking.
You can used just that simple code:
d: & activate your_env & jupyter notebook
It's really working for me
I found an alternative way to solve this problem without a .bat.
Search your start menu for "Jupyter Notebook". You should find a shortcut called:
"Jupyter Notebook (environmentname)"
This short cut was created when I set up my environment. In my case environmentname is py35.
To change the directory that Jupyter Notebook starts in find the "Anaconda Prompt" shortcut. Then open "Properties>Shortcut" and change the "Starts in:" field to your desired directory.
If you did not add conda to your path, the following script might be helpful for you. According to https://docs.anaconda.com/anaconda/install/multi-user/ you might even want to check C:\ProgramData\Anaconda but I didn't want to add something to the Batch script that I haven't checked myself.
:: Starts JupyterLab
SET CONDA_ENV=my-specific-conda-environment-name
where conda 2> nul
IF ERRORLEVEL 1 (
ECHO "Conda is not available in your PATH. Guessing the location of the installation..."
CALL C:\Users\%USERNAME%\Anaconda3\Scripts\activate %CONDA_ENV% || (
ECHO "Either Conda or the environment was not found."
PAUSE
)
) ELSE (
CALL activate %CONDA_ENV%
)
CALL jupyter lab || (
ECHO "JupyterLab encountered an error, please check the error message"
PAUSE
)
here is the batch file for this problem
cd directoryname
call activate envName
start jupyter notebook
This is what I use to activate my virtual environment, then run my notebook.
#echo off
call env\Scripts\activate
call jupyter-lab
Saved this as a .bat, so all I have to do to open the notebook is run it.
The best solution I found so far, which works without creating a custom batch file:
Navigate to activate.bat in C:\Users\<youruser>\<Anaconda3 or miniconda3>\Scripts\activate.bat
Right click and create a link to the activate.bat from the Desktop
Right click to change property of the link in the tab link
In Target you enter: %UserProfile%\<Anaconda or miniconda3>\Scripts\activate.bat <your-env-name> && #CALL jupyter lab (when you do not know your environment name you can check them in the file environments.txt in C:\Users\<youruser>\.conda
In the field below you enter your path to the project folder (not to the environment), this is where the script will start.
After this you can edit the icon / title / .. of the link and execute.
This is my code. It starts jupyter notebook in the directory it is saved:
#echo on
cd 'your_path_to_anaconda' /condabin
call activate.bat
cd %~dp0
call jupyter notebook
cmd \k
I am missing something basic, I'm sure. If I have a jupyter notebook at a url like this https://github.com/nicolaskruchten/pyconca/blob/master/jupyter_magic.ipynb, how can I view it as an interactive notebook that I can then edit?
You can load it from a few places online and of course offline. The easiest way to get this loaded offline is downloading Python + Jupyter and that can be found in one place. Download and install Anaconda (2.7 or 3.4) from http://continuum.io/downloads and depending on your OS,
If you run mac or linux restart a terminal and run "ipython notebook" which will open a browser to navigate you to the right location (your downloads folder) to find the .ipynb file
if you run Windows there is an icon in the applications menu to start "Ipython notebook" which will open a browser to navigate you to the right location (your downloads folder) to find the .ipynb file
Online there is a site called http://Wakari.io/ (requires an account) from which you can share the result with others.
I have recently installed the Anaconda distribution on Windows 7 (Anaconda 3-2.4.0-Windows-x86_64). Unlike IDLE, I can't right-click and open a py file in the Spyder IDE. I will have to open Spyder first and then navigate to the file or drag and drop it in the editor. Is there any way to open the file in the editor directly from Widows Explorer?
With the current version of Anaconda (4.1.0) you can simply right-click on a python script in Windows File Explorer and choose "Open with". The first time you do this you need to select "Choose default program" and then browse to spyder.exe in the Script directory in your Anaconda installation. Also make sure that the "Always use the selected program to open this kind of file" is unchecked and then click OK. From now on spyder.exe will always be listed as one of the options when you select "Open with" from the right-click menu in Windows File Explorer.
I have had a similar problem with another piece of software that I use.
My work around for this problem is to set the file association for .py files to C:\Anaconda\Scripts\spider-script.py via the Open with dialog. If you now try to open your File.py by double clicking you'll receive an error like
~\file.py is not a valid Win32 application.
This can be resolved by editing the spyder-script.py registry key:
HKEY_USERS\S-1-5-21-3559708500-1520960832-86631148-1002\Software\Classes\Applications\spyder-script.py\shell\open\command
and replacing the default value "C:\Anaconda\Scripts\spyder-script.py" %1 with "C:\Anaconda\python.exe" "C:\Anaconda\Scripts\spyder-script.py" %1. Use the search function for this key if the path isn't the same for your machine, and of course use the appropriate path for your python installation. spyder-script.py should now execute in a python shell.
From the docstring of ftype,
...Within an open command string, %0 or %1 are substituted with the file name being launched through the association.
(Spyder maintainer here) This functionality is available as part of our Windows installer. In other words, if you install Spyder with it, then you'll see a new entry in the Open with menu of the Windows Explorer that allows you to open Python files directly on Spyder.
Unfortunately, it's not possible for us to do the same for other installation methods (i.e. when using Anaconda or pip). That's why it's necessary to resort to different hacks, as the ones mentioned in other answers here, to have this functionality for them.
What is working very well for me in Windows (10), is associating the *.py files with a batch file (let's say "SpyderBATCH.bat") containing this line :
[ANACONDA_FOLDER_PATH]\pythonw.exe" "[ANACONDA_FOLDER_PATH]\cwp.py" "[ANACONDA_FOLDER_PATH]" "[ANACONDA_FOLDER_PATH]/pythonw.exe" "[ANACONDA_FOLDER_PATH]/Scripts/spyder-script.py" %1
Where [ANACONDA_FOLDER_PATH] has to be replaced with the full path to the Anaconda folder (usually under "Program Files").
What Windows does, when double-clicking on a python script (let's say "file.py"), is pass to SpyderBATCH, as parameter number %1, the full path to "file.py".
Then Spyder is launched and displays the script "file.py" in the editor view.
I figured I would post my solution for this as well.
I have Spyder installed in multiple different environments. You can't simply call the spyder-script.py script without errors, because the environment must be activated.
#echo off
call [YOUR_CONDA_PATH]\Scripts\activate.bat [YOUR_CONDA_PATH]
call conda activate [YOUR ENVIRONMENT]
call start [YOUR_CONDA_PATH]\envs\[YOUR ENVIRONMENT]\pythonw.exe "[YOUR_CONDA_PATH]\envs\[YOUR ENVIRONMENT]\Scripts\spyder-script.py" %1
You can remove the second line and remove the environment extension from the third line if you have Spyder installed in your base environment.
Hopefully for anyone experiencing any weirdness with the other solution, this one will do the trick by activating the environment correctly.
The solution from JoeB152 worked perfectly for me!
If you are interested in adding the spyder icon (or any other) to the .py-files and if you would like to avoid the cmd-pop-up, I found out the following workaround which is feasible without admin rights:
Download the portable version of Bat To Exe Converter (I used v3.0.10).
Open your custom .bat-file in the Bat to Exe Converter.
In the options settings, activate "Icon" and give the path to the respective icon (for me it's in: .../AppData/Local/Continuum/anaconda3/Scripts/spyder.ico).
Set Exe-Format to Invisible (no empty cmd window would pop up anymore)
Convert your .bat-file to an .exe-file.
As usual, set to open .py-files with the newly created .exe.
Enjoy!
Environment:
Windows 10,
Conda 4.8.2,
Spyder 4.0.1,
Python 3.7
This problem is related to anaconda installation defaults - it does not register itself in PATH by default an dicourages users to do so. After proprly registering all directories in path, spyder.exe works as expected.
How to know, what to register?
locate and activate.bat an run it in cmd, then run echo %PATH% and manually register all directories mentioning anaconda.
Alternatively, reinstall anaconda with PATH registratin enabled.
Then you can associate .py files wit spyder.exe and association will work.
System: Windows 11, Python 3.9.7 (Installed through Anaconda3)
This solution will allow you to double click .py files and have them open in the Spyder in the environment of your choice, but does NOT associate .py files with the icon for Spyder.
I'm answering because it took me over an hour to understand & piecemeal together all the (great) solutions that are provided in this thread to get something that works (thanks Martin Sorgel, JoeB152 and Max-K).
NOTE: Some commentors above say to use a Bat-to-Exe converter & that will let you get the icon too.. but, DON'T! All of the ones you're finding via google have got some bad malware in them and my computer ended up deleting the .exe's I was making using that method because they had a Trojan in them.
Full Solution:
STEP 1: Make a .bat file that will launch Spyder in a specific environment.
1.1 Open a plain text editor (e.g. Notepad, etc.) and copy/paste the text below into it.
#echo off
call [YOUR_CONDA_PATH]\Scripts\activate.bat [YOUR_CONDA_PATH]
call conda activate [YOUR ENVIRONMENT]
call start [YOUR_CONDA_PATH]\envs\[YOUR ENVIRONMENT]\pythonw.exe "[YOUR_CONDA_PATH]\envs\[YOUR ENVIRONMENT]\Scripts\spyder-script.py" %1
Update [YOUR_CONDA_PATH] in the text above with the path to Anaconda3 on your computer. Mine was C:\Users\myusername\Anaconda3\ & yours is likely similar.
1.2 Save this new file as spyderlaunch.bat and place it on your computer somewhere that you'll NEVER move it (otherwise you'll have to do STEP 2 each time you move this file. I save mine in a python_env_settings folder where I keep info on what packages I installed manually in my different environments).
NOTE: JoeB152 says you can remove the second line and remove the environment extension from the third line of the text above if you have Spyder installed in your base environment. I'm not sure if this works...
1.3 Make sure your new .bat files works! It works if when you double click on spyderlaunch.bat, that it launches and opens Spyder in the environment you want it to! (Spyder will show the environment it opens in on the bottom right hand side: ).
STEP 2: Tell your computer to associate (i.e. open) all .py files with the spyderlaunch.bat file you just created.
2.1 Open an Anaconda Terminal with "run as an administrator" (by right clicking on the application) and run the following 2, separate commands. Update[PATH_TO_YOUR.batfile] to wherever you saved spyderlaunch.bat in 1.2.
assoc .py=Python
ftype Python="[PATH_TO_YOUR.batfile]" "%1" %*
Errors?
If you don't run the Anaconda Terminal application as an administrator you will be denied access to associate .py=Python. If that's not your issue, then check that the spaces and quotation marks are exactly where they appear above. In particular, you may want to make sure there is a space in between the quotation marks around [PATH_TO_YOUR.batfile] and those around %1.
I was unable to find a spyder.exe on my installation of conda. However in my users/.anaconda/navigator/scripts I found a spyder.bat file. Using this to open the file opens an anaconda prompt and shortly after spyder will open the file. The file icon is broken but it works for me. Hope this might help.
(Comment in relation to the responses by JoeB152 and Jessica Haskins - I am new, so I cannot leave comments)
I found that their suggested .bat file works once you copy-paste the following file from A to B:
A) C:\Users\USERNAME\Anaconda3\Scripts\spyder-script.py
B) C:\Users\USERNAME\Anaconda3\envs\ENVRIONMENT_NAME\Scripts\
...where ENVIRONMENT_NAME is the name of your environment, such as main or test.
The .bat file contains:
#echo off
call C:\Users\bloggsj\Anaconda3\Scripts\activate.bat C:\Users\bloggsj\Anaconda3\
call conda activate C:\Users\bloggsj\Anaconda3\
call start C:\Users\bloggsj\Anaconda3\envs\main\pythonw.exe "C:\Users\bloggsj\Anaconda3\envs\main\Scripts\spyder-script.py" %1
Then associate .py files with that .bat file (e.g., via the 'Open with...' dialogue).
Alternatively, you could try using in the last line of the .bat file the file path: "C:\Users\bloggsj\Anaconda3\Scripts\spyder-script.py"
Get Spyder by itself:
https://docs.spyder-ide.org/current/installation.html
Set your default file opener to your newly installed spyder
To be able to add packages:
Make sure Anaconda is installed.
Go to Spyder preferences
Go to Python interpreter
Select: "Use the following Python interpreter"
Select file path with Anaconda and hit apply
Now you should be able to open files directed using Spyder and update your environment using Anaconda.
I made a batch utility spy.bat for this (not in the Anaconda directory)
spy.bat
call E:\Anaconda3\Scripts\activate.bat E:\Anaconda3
e:\Anaconda3\python.exe e:\Anaconda3\Scripts\spyder-script.py %1
Then, I associated the .py file type in Windows with this batch file as:
Right click any .py
Choose 'Open With' -> 'More Apps' -> 'Search on your PC' ->'select spy.bat'
In the spy.bat:
E:\Anaconda3 is my Anaconda installation directory.
First line starts the environment (Anaconda prompt).
Second line starts the spyder-script. %1 is to open the pyscript you want to open.