I'm looking for a way to put my startup script in Jupyter. In IPython, I put it under the $IPYTHON_DIR/profile_default/startup/.
In Jupyter, it seems that the config file should be $JUPYTER_CONFIG_DIR/jupyter_notebook_config.py. However, I would like to use my startup file, which import a slew of Python libraries at the launch of the kernel.
Where can I put such file in Jupyter?
You can get the default startup script folder via this in jupyter notebook:
get_ipython().profile_dir.startup_dir
On my Windows PC, the folder is: C:\Users\myusername\.ipython\profile_default\startup
And do read the README file under the folder.
Files will be run in lexicographical order, so you can control
the execution order of files with a prefix, e.g.::
00-first.py
50-middle.py
99-last.ipy
So you can put a file named 00-xx.py under the folder.
Related
For background, I am using the Atom editor with the script package installed.
Within Atom, there are two projects that I normally use.
D:/Data_files/Git/Projects
D:/Data_files/Python/Notes
When I normally open atom from the terminal, I have both projects open, and for whatever reason it has the default project or something set to the Git/Projects project
The Git/Projects directory is not important, but here is what the Python directory looks like.
D:/Data_files/Python/Notes/
__init__.py
files_and_exceptions.py
text.txt
Here's what text.txt contains
This is the text of text.txt
Here is what files_and_exceptions.py contains
import os
print(os.getcwd)
os.chdir("../../Python/Notes")
print(os.getcwd)
with open('text.txt') as hello:
contents = hello.read()
print(contents)
Which outputs when I run it using CTRL+SHIFT+B from the files_and_exceptions.py using script
D:/Data_files/Git/Projects
D:/Data_files/Python/Notes
This is the text of text.txt
So even though I'm running it from the Python/Notes directory, the current working directory is set to Git/Projects. I want files_and_exceptions.py to print text.txt without importing and changing directories when running from the file because that's how it would work when I run it normally using Python's Shell. I know I can set it by opening Atom from the directories in separate windows but that gets irritating when I'm working with multiple projects at once. Is there any way to easily change the current working directory of Atom when working between projects or modify the script package to change the current working directory to the directory of the open tab (ie: files_and_exceptions.py) when I run it with CTRL+SHIFT+B?
In the default settings, script uses the project directory of the first project in your Project tree. In the settings of script, you can change the that the project directory of your current script (or the directory your script resides in itself).
You go to settings (press Ctrl+,), then the Packages tab, you search for the script package and the first dropdown menu is that setting.
If you don't want to change this, you can also have script run with a specific profile.
In the command palette Ctrl+Shift+p, you search for script: Run options and you get a menu with some settings to start your script with. You can save those as profile and later use script: Run with Profile to reload.
I am very new to this and struggling with the basics. I have a csv file /home/emily/Downloads/Roger-Federer.csv The textbook says that I need to "extract the file and download it to my current directory" on JupyterLab (I am using Python). What does this mean? How do I do this? Thank you
Every running program, including JupyterLab, has a "working directory" which is where it thinks it is on your computer's file system. What exactly this directory is usually depends on how you launched it (e.g., when you run a program from terminal, its working directory is initially the folder your terminal was in when you ran the command, but it's possible for a program to change its own working directory later).
Your file path indicates you're on Linux, so I'd suggest opening a terminal in your JupyterLab and running pwd to have it print out its current directory. (You can also run !pwd in any open notebook if that's easier.) You should then copy your CSV file to that directory.
If you do that, then from your Python code, you can just open the file locally, like open('Roger-Federer.csv') or pandas.read_csv('Roger-Federer.csv'). You don't have to move the file to open it from Python, though, you can just give it the entire file path, like open('/home/emily/Downloads/Roger-Federer.csv'), and that'll work just fine too.
I'm new to programming and now taking the course MIT 6.0001. On problem set 2, they set a .py file, accompanied with a .txt file.
I ran .py file in Spyder with no difficulties (.txt file embedded just fine).
But when I run .py file in Jupyter, things won't run (as in screenshot).
I want to use Jupyter because it's much clearer than Spyder, and can give headers to cells to keep myself on track. Please help me to run things in Jupyter.
Here are link to the problem set (ZIP file)
File not running in Jupyter
Use
%load filename.py
to import the text of your python file into a cell in a Jupyter notebook and then save that notebook.
Jupyter isn't built to run/edit .py files, it's built to run/edit jupyter notebooks (ipython notebooks) (.ipynb)
I'm using WingIDE for development and Ipython for running my scripts. I'm facing some inconvenience on several points:
Whenever I update my code, I have to reload my module in order to update it in IPython. To solve that I followed Jomonsugi's answer in the following link: How to automatically reload modules in IPython?
and it works.
Each time I launch IPython, my path is set to my exe's location and I have to cd to my script's directory.
I tried to change directory automatically when launching IPython by setting the parameter c.InteractiveShell.ipython_dir = <mypath> in ipython_config.py but it doesn't work.
Is it possible to set the default path? Is there a particular parameter to set in ipython_config.py?
One way is to use your startup.py file. It should be located somewhere like:
C:/Users/yourname/.ipython/profile_default
If it's not there already, create it. Then use
import os
os.chdir('C:/Users/mypath')
Those two lines will then be run at IPython startup.
Source: IPython.org > Startup Files
I’m sure Brad Solomon’s answer is right for his version of IPython, but I’ve just downloaded IPython with pip install ipython and my startup files are in a directory nested one deeper than his. My IPython version is 7.18.1, and the start-up files are located in ~/.ipython/profile_default/startup/. There is a README there which states
This is the IPython startup directory
.py and .ipy files in this directory will be run *prior* to any code
or files specified via the exec_lines or exec_files configurables
whenever you load this profile.
Files will be run in lexicographical order, so you can control the
execution order of files with a prefix, e.g.::
00-first.py
50-middle.py
99-last.ipy
That’s pretty self-descriptive, but I would add to this (for anyone coming here from here) that %load_ext autoreload is an IPython command, so you might want to create a file startup.ipy with contents something like this:
%load_ext autoreload
%autoreload 2
print('IPython startup file - created 2020/9/28')
I run Jupyter Notebook on a mac and I am trying to edit a notebook file from a git repo I am contributing to. However I am having trouble accessing the file from the Notebook interface.
Is there a way to access notebooks which are not in the default path on mac (navigation to arbitrary file locations works fine on windows) without changing the default directory to my git repo and without copying the file to my default directory and back every time I edit it?
It looks like direct navigation outside the path is impossible (Based on this post) but it seems that it should be possible to start the notebook from the given directory using the command line.
Things I've tried:
Directly typing the relative and absolute path into the web interface with respect to the tree (i.e. http://localhost:8889/tree/../../[path-to-file]
Renaming the file with the path as a prefix
Starting jupyter from the directory containing the notebook
... with no success as of yet.
Any help on this would be greatly appreciated.
it seems that it should be possible to start the notebook from the given directory using the command line
You can...
jupyter notebook /some/non-default/path
However, you can only reach sub-directories of that path with Jupyter
Starting jupyter from the directory containing the notebook
That should also work
If you python code needs to read files that are in parent directories, relative paths still resolve fine within the notebook kernel.