How to change working directory in Jupyter Notebook? - python

I couldn't find a place for me to change the working directory in Jupyter Notebook, so I couldn't use the pd.read_csv method to read in a specific csv document.
Is there any way to make it? FYI, I'm using Python3.5.1 currently.
Thanks!

Running os.chdir(NEW_PATH) will change the working directory.
import os
os.getcwd()
Out[2]:
'/tmp'
In [3]:
os.chdir('/')
In [4]:
os.getcwd()
Out[4]:
'/'
In [ ]:

You may use jupyter magic command as below
%cd "C:\abc\xyz\"

First you need to create the config file, using cmd :
jupyter notebook --generate-config
Then, search for C:\Users\your_username\.jupyter folder (Search for that folder), and right click edit the jupyter_notebook_config.py.
Then, Ctrl+F: #c.NotebookApp.notebook_dir ='' . Note that the quotes are single quotes. Select your directory you want to have as home for your jupyter, and copy it with Ctrl+C, for example: C:\Users\username\Python Projects.
Then on that line, paste it like this : c.NotebookApp.notebook_dir = 'C:\\Users\\username\\Python Projects'
Make sure to remove #, as it is as comment.
Make sure to double slash \\ on each name of your path.
Ctrl+S to save the config.py file !!!
Go back to your cmd and run jupyter notebook. It should be in your directory of choice. Test it by making a folder and watch your directory from your computer.

on Jupyter notebook, try this:
pwd #this shows the current directory
if this is not the directory you like and you would like to change, try this:
import os
os.chdir ('THIS SHOULD BE YOUR DESIRED DIRECTORY')
Then try pwd again to see if the directory is what you want.
It works for me.

it is similar to jason lee as he mentioned earlier:
in Jupyter notebook, you can access the current working directory by
pwd()
or by import OS from library and running os.getcwd()
i.e. for example
In[ ]: import os
os.getcwd( )
out[ ]: :c\\users\\admin\\Desktop\\python
(#This is my working directory)
Changing Working Directory
For changing the Working Directory (much more similar to current W.d just you need to change from os.getcwd() to os.chdir('desired location')
In[ ]: import os
os.chdir('c:user/chethan/Desktop') (#This is where i want to update my w.d,
like that choose your desired location)
out[ ]: 'c:user\\chethan\\Desktop'

It's simple, every time you open Jupyter Notebook and you are in your current work directory, open the Terminal in the near top right corner position where create new Python file in. The terminal in Jupyter will appear in the new tab.
Type command cd <your new work directory> and enter, and then type Jupyter Notebook in that terminal, a new Jupyter Notebook will appear in the new tab with your new work directory.

Jupyter under the WinPython environment has a batch file in the scripts folder called:
make_working_directory_be_not_winpython.bat
You need to edit the following line in it:
echo WINPYWORKDIR = %%HOMEDRIVE%%%%HOMEPATH%%\Documents\WinPython%%WINPYVER%%\Notebooks>>"%winpython_ini%"
replacing the Documents\WinPython%%WINPYVER%%\Notebooks part with your folder address.
Notice that the %%HOMEDRIVE%%%%HOMEPATH%%\ part will identify the root and user folders (i.e. C:\Users\your_name\) which will allow you to point different WinPython installations on separate computers to the same cloud storage folder (e.g. OneDrive), accessing and working with the same files from different machines. I find that very useful.

Open jupyter notebook click upper right corner new and select terminal then type cd + your desired working path and press enter this will change your dir. It worked for me

list all magic command %lsmagic
show current directory %pwd

I have did it on windows machine. Detail mentioned below
From windows start menu open “Anaconda Prompt
Find .jupyter folder file path .
In command prompt just type
or to find the .jupyter path
After find the .jupyter folder, check there has “jupyter_notebook_config” file or not. If it is not there then run below command
After run the command it will create "jupyter_notebook_config.py"
if do not have administrator permission then Some time you could not find .jupyter folder . Still you can open config file from any of the text editor
Open “jupyter_notebook_config.py” file from the the “.jypyter” folder.
After open the file need to update the directory is use for notebooks and kernel. There are so many line in config file so find “#c.NotebookApp.notebook_dir” and update the path
After:
Save the file
6. Now try to create or read some file from the location you set

What works for me on Windows is creating a shortcut to jupyterlab, and altering the properties of the shortcut.
At "Start in:", enter your desired working directory.
Using this method, you don't have to configure a global file either.

Related

Wrong current working directory when running python code and jupyter extension in vscode

I am trying to run python code using the jupyter extension in vscode here
I have a workspace open at this location
/Users/user/Documents/
When I try and run the following code in a file called test.py in a child directory, the current working directory is set at the workspace level rather than the file. Is it possible to change a setting to use the cwd of the file, rather than the workspace? I cannot find one in settings.json and the "cwd" in launch.json only seems applicable to debugging.
File location:
/Users/user/Documents/python_code/test.py
#%%
import os
print(os.getcwd())
Expected output:
/Users/user/Documents/python_code/
Actual output:
/Users/user/Documents/
When running the same code through the terminal it prints the expected result, so the issue seems to be related to the jupyter extension
For any specific folder / workspace that you have open in VS Code you can use the notebookFileRoot setting to set a specific absolute path directory to always set the Jupyter working directory to when you start the Interactive Window with that folder open.
Always opening on the file location (without having to set notebookFileRoot to an absolute path per folder) is not supported via the notebookFileRoot setting. The VSCode variables such as ${fileDirname} are specific to task and debug configuration files (launch.json and task.json). We've specifically added code to recognize ${workspaceFolder} for our settings page but we don't recognize other VSCode variables in there.
If you want there is a github item here that has suggested this feature. You can follow that or vote it up if you want this feature added.
https://github.com/Microsoft/vscode-python/issues/4441
Edit 11/5/2019:
We have now made a change to allow the VS Code "Notebook File Root" setting to be set to ${fileDirname}. This setting will now start up Notebook Editor and Interactive Window session relative to the file location that was used to start them.
There is the setting python.dataScience.notebookFileRoot which is, as far as I understand, supposed to achieve the expected behaviour when setting it to ${fileDirname}.
See correspoding source.
However, it does not seem to work in my case. Maybe a bug?
Note that the output of running the script from the terminal depends on you working directory of the terminal!

How to find path to current .py file in Spyder (Anaconda)?

Set-up
I run a script on my computer, located in the directory Users/path/to/my/script.py.
In the script, I use the path to the script, e.g.,
sub_path = 'Users/path/to/my/'
os.chdir(sub_path + 'other_script/')
As you can see, I define sub_path in the code 'manually'.
Problem
I don't want to define the sub_path manually, I'd rather have Python do it for me.
I'm looking for something similar to the code I use to obtain the current working directory: os.getcwd(), but then a code to obtain the directory of the current file.
I mainly find answers similar to this one, which says,
os.path.abspath(os.path.dirname(__file__))
but in the Spyder & Anaconda set-up, this generates a NameError: name '__file__' is not defined.
What can I do?
You if you want to move back one folder/directory you use the .. in your file path.
os.chdir('../other_scripts/')
will work. You may fine it helpful to view this or the wiki.
If you want to move from where you currently are you can use './new_dir/'. If you want to automate how to find other files you may want to read here which says to use os.walk. This may be the same question.
Mark8888 pointed out to run the whole script (run file (F5)) instead of just pieces of the script
this way multiple approaches should work to get the script file location and change the current working directory
import os
# directory of script file
print(os.path.abspath(os.path.dirname(__file__)))
# change current working directory
os.chdir(os.path.abspath(os.path.dirname(__file__)))
# current working directory
print(os.getcwd())
also
import os
import sys
# directory of script file
print(os.path.abspath(os.path.dirname(sys.argv[0])))
# change current working directory
os.chdir(os.path.abspath(os.path.dirname(sys.argv[0])))
# current working directory
print(os.getcwd())
I add the following lines to any script I run in case I need to access data relative to the location of the script
import sys
script = sys.argv[0]
print(script)
'C:/SomeFolder/A_Subfolder/CurrentlyRunningScript.py' # changed obviously
First, save your Jupyter Notebook. Second, locate the directory your Jupyter Notebook is stored in. Thirdly, ensure that your Jupyter Notebook and CSV file are in the same place.

How to use Anaconda Python to execute a .py file?

I just downloaded and installed Anaconda on my Windows computer. However, I am having trouble executing .py files using the command prompt. How can I get my computer to understand that the python.exe application is in the Anaconda folder so it can execute my .py files?
You should use Anaconda Prompt instead of common Windows command prompt.
Then navigate to your folder with the .py file and run:
python myfile.py
However if you want to use normal command prompt you should put the path with you're python.exe which is usually in
C:\Users\<username>\AppData\Local\Continuum\anaconda3\python.exe
behind this one put your .py file.
Launch JupyterLab from Anaconda
(Perform the following operation with JupyterLab ...)
Click on icon folder in side menu
Start up "Text File"
Rename untitle.txt to untitle.py (The name of the file started up was also changed)
Start up the "terminal" (In windows the power shell starts up)
Execute the command python untitle.py
Right click on a .py file and choose 'open with'
Scroll down through the list of applications and click something like 'use a different program'
Naviage to C:\Users\<username>\AppData\Local\Continuum\anaconda3
click on python.exe and then click on 'ok' or 'open'
Now when you double click on any .py file it will run it through Anaconda's interpreter and therefore run the python code.
I presume if you run it through the command line the same would apply but perhaps someone could correct me?
Just get to the home of jupyter notebook and select "New" then select "Text file".
Then save the text file as file_name.py
Write your code in the file and save the file.
Then open the "Anaconda Prompt" and then type as follows to run your file
python file_name.py
You can do it from the "Anaconda Prompt"
conda run "my_script.py"
I was doing exactly as Martin Bosch suggested, and was getting the following:
(base) C:\>python command.py
python: can't open file 'command.py': [Errno 2] No such file or directory
I solved it this way:
navigate to the exact file location using the "cd" command
for me this was:
(base) C:\>cd my_scripts
this should put you specifically in the file where your .py script is located.
now you should try to input the name of your file.
(base) C:\my_scripts> test_script.py
you may get asked which program to run this with, and simply find python.exe
After doing this process once, I can simply type (in anaconda prompt)
test_script.py
and it runs no problem, even from the top of the file tree (I don't have to be in the exact file, nor do I have to explicitly give the whole file path)
Anaconda should add itself to the PATH variable so you can start any .py file with "python yourpythonfile.py" and it should work from any folder.
Alternatively download pycharm community edition, open your python file there and run it. Make sure to have python.exe added as interpreter in the settings.
If you get the following error:
can't open file 'command.py': [Errno 2] No such file or directory
Then follow this steps to fix it:
Check that you are in the correct directory where the Python file is.
If you are not in the correct directory, then change the current working directory with cd path. For instance: cd F:\COURSE\Files.
Now that you are in the directory where your .py file is, run it with the command python app.py.
Check where is the directory for the ananconda environment directory which is generally
"C:\Users\[UserName]\.conda\envs\[conda environment directory]"
You will see python.exe in that directory.
After that, you need to use the following command to execute your python file (i.e. xx.py) when you are running Anaconda prompt and you will be done:
"C:\Users\[UserName]\.conda\envs\[conda environment directory]\python.exe" xxx.py
BTW, if you have global variable (i.g. variable yyy) that contain directory, you have to define the global variable that contains full path of directory just below the header (the import section) to prevent the "name 'yyy' is not defined" error to occur:
from pathlib import Path # dealing with path issue
yyy = Path("[DriverLettter]:\Full\Path\of\Directory")

Navigating Directories in Python

Im trying to navigate directories in python like this, and for some reason its telling me that
IOError: File ../Harvard%20Stats%20Course%20Data%20Files/diamonds.csv does not exist. The file does exist, however, one folder out, and then in the Harvard Stats Course Data Files folder.
diamond_data = pd.read_csv('../Harvard Stats Course Data Files/diamonds.csv',
sep = ',', index_col=0)`
Any advice would be appreciated.
At the IPython prompt, type pwd. That will show you the current working directory. Perhaps it is not the directory you think it is. You can change the current workding directory by typing cd /path/to/dir at the IPython prompt.
Alternatively, you could simply supply an absolute path to the CSV file.
Using ../ like that will be relative to wherever you're running the program from. Try to change into the directory of the .py file and run it again.

Set Working Directory to Notebook Directory

In an IPython nb, is it possible to set programmatically the working directory to the directory of the notebook?
For example, the following code will work in a regular .py file.
import os
# show working dir
os.chdir(os.path.abspath('/'))
print "initial working directory:\t", os.getcwd()
# get path of script file
scriptPath = os.path.abspath(os.path.dirname(__file__))
# change working dir to dir with script file
os.chdir(scriptPath)
# show working directory
print "final working directory:\t", os.getcwd()
However, I can't find the equivalent of the
__file__
variable for a ipython nb file. Is there some equivalent approach for ipynb files?
iPython notebook appears to automatically switch the directory to the same one as the .ipynb file. Do you want to change out of that directory and then change back later? If so, just store the original directory at the start of the program and use it whenever.
import os
orig_dir = os.getcwd()
os.chdir('/some/other/directory')
#do stuff
os.chdir(orig_dir)
EDIT: There appears to be a special variable _dh, which is a list such that _dh[0] contains the name of the directory in which the iPython kernel was started. I only just discovered this, so I'm not sure that this will be robust to a SaveAs either (I can't find a way to do this in my version of iPython). However, it doesn't change when I do os.chdir(), so I suspect that at least the first element of the list always contains the notebook's directory.

Categories

Resources