Open .ipynb in Anaconda Jupyter Notebook on double-click (macOS) - python

I've been trying to figure out how I can open a .ipynb file on double-click on a Mac, so the file opens with Anaconda and then automatically opens Jupyter Notebook with the file contents.
I have tried creating an application through Automator that opens with a jupyter_lab command, but the issue is that Jupyter lives in Anaconda, so this method is not working. See these articles for my steps: https://samedwardes.com/2020-01-31-open-ipynb-with-double-click/ and Open an ipython notebook via double-click on osx
I have also tried installing nbopen with the following:
python3 -m pip install nbopen
This works, and then I can run nbopen file.ipynb through the terminal, but I want to be able to bypass this step and do it while in file explorer, physically looking at the file instead.
I have ran the recommended command for osx to integrate it with file manager ./osx-install.sh but I just get zsh no such file or directory and can't really find any help with figuring out why this is pushing back an error.
Any suggestions?

I have this "convenience issue" as well, and didn't go for the nbopen route, but instead, I just made a batch script (I named it jupyter-notebook.bat) that calls certain conda functions, and pretty much initiates things like how double clicking works. In the script, I just have this:
call "C:\Users\XYZ\Anaconda3\Scripts\activate.bat"
call conda activate myEnvironment
call python C:\Users\XYZ\Anaconda3\Scripts\jupyter-notebook-script.py %1
Lastly you just need to configure that every .ipynb opens up using your jupyter-notebook.bat script.
For consistency, I placed the script in my Anaconda folder. And if you also have nb_conda_kernels installed in your base environment, you'd have access to the other environment you've created as well from there.

Related

Can´t make jupyter notebook work in Anaconda

On my windows PC, i have Anaconda installed, but today i could not open Jupyter Notebook correctly. When i use these commands to set up an enviornment, i can open the Jupyter notebook.
conda activate [name_of_the_environment]
jupyter notebook
The error occurs when internet explorer opens the Jupyter notebook, because i can't access to my ipynb files or any other. The page just stays like this (in blanc, like it does not end loading):
Internet explorer general site
Trying to open a file
I am not having any connection problems and this happens regardless of the file size.
I also tried updating Anaconda, but it didn´t work.
Code written in the terminal:
Code

Jupyter in VSCode

enter image description here
At first, jupyter notebook works fine. BUT, whenever I try to use the other ipynb file, jupyter notebook does not work and send this message. How should I deal with this?
Have you checked which python interpreter you are using in the Jupyter Notebook?
With the different names of the .ipynb file, it will take the different kernels, and it is stored in the database.
First of all, create a virtualenv. Open the terminal using Ctrl+Shift+`.
In the console, type python3 -m venv venv.
This will create a folder named venv in your working directory.
Now, in the bottom left corner, you will find the selected python interpreter. Click that and change it to venv.
(If windows raises permission error, In your vsCode settings, add the following lines.)
"terminal.integrated.shellArgs.windows": ["-ExecutionPolicy", "Bypass"]
Now delete all previous terminals and open a new terminal.
Install the required packages using pip and carry on.

How to directly open Jupyter from cmd instead of copying link

I often use jupyter for classes, and use the commmand python -m notebook to open the notebook. The tab that opens up shows an error, and then I have to copy one of the links in the terminal window instead. Is there a way or a command that I can use so that one of those links automatically opens up?
NOTE: It opens the file location (first address in the picture) and fails, and I use the links below to open jupyter. I want the terminal to open the links directly if possible to save time.
This is the error that I get.
jupyter notebook notebook.ipynb
Taken from this documentation.
It shouldn't matter whether you install it using conda or pip.
Or you can just type jupyter notebook which should open up a your file directory in the browser and let you navigate to the notebook file you want to open.
Edit: For this error, it really is hard without knowing the full context of the commands you are running, the directory you are calling from, and where this file you want to open is located. Make sure you are in the folder that contains your notebook file. If not, you should specify the entire path to the notebook file. Finally, does the command jupyter notebook by itself work?

which app does anaconda used to run .py scripts

I was trying to run dronekit-SITL in windows so I downloaded the anaconda so that I can use py-2.7 separately, but when I try to run mavproxy.py script, it shows up a dialog box to choose an app to run the .py file, and when I click on any of the apps nothing happens.
this will show what my problem is actually
Jupyter Notebook
it will be installed along with anaconda

How to get Spyder to open python scripts (.py files) directly from Windows Explorer

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.

Categories

Resources