Jupyter Lab/Notebook magic command %load with platform independent path - python

I am trying to develop a Jupyter notebook that includes cells that have the %load magic command to load code from elsewhere. This code is not in the same directory as where the notebook is. I want this to work on Windows, Linux and Mac, so path separators should sometimes be '\' and sometimes '/'.
Usually I would solve this by using os.path.join. Nevertheless, when I do this in a line with the load command, the notebook just evaluates the path, and doesn't actually load the code. Is there a way of doing this, other than first just changing the working directory and changing it back after executing the code that I loaded?
Brief example:
import os
%load os.path.join('example', 'file.py')
This gives an error as it will actually search for a file with the name os.path.join('example', 'file.py'). If I first evaluate that and put the result in load I get:
import os
to_include = os.path.join('example', 'file.py')
print(to_include)
%load to_include
That evaluates to just
# %load to_include
example/file.py
But obviously I want the content of that file loaded, not the path + filename. What am I doing wrong?

In Jupyter you have to expand variables in a bash-like syntax for them to work in magic functions.
That's why you will have to use the $ sign.
In your case:
import os
to_include = os.path.join('example', 'file.py')
%load $to_include

Related

Launching an excel file with os.system() won't work properly

I am having issues with opening an Excel file from Jupyter Notebooks on my Mac. I previously saved it to my cloud.
To create the Excel, I copy my df to Excel and save it. So far so good.
path = '/Users/username/name.xlsx'
writer = pd.ExcelWriter(path,
engine = 'xlsxwriter')
df.to_excel(writer, sheet_name = 'Sheet1')
writer.save()
writer.close()
When I then try to launch it, it won't work.
Here's my code:
import os
os.system("open -a '/Applications/Microsoft Excel.app' path")
All I get is Output 256 in my notebook and Excel won't open.
Would anyone know why?
You need to pass the Python string stored in path as a value into the shell system in a form it will use.
An option is to use f-strings to do that:
import os
path = '/Users/username/name.xlsx'
os.system(f"open -a '/Applications/Microsoft Excel.app' {path}")
Alternatively, you can drop use of os.system and use IPython/Jupyter's ability to run shell commands on a line in a cell where you start with an exclamation point. That has built in that it will pass in Python variables for bracketed items in the call to the shell.
So in your Jupyter cell, you put:
path = '/Users/username/name.xlsx'
!open -a '/Applications/Microsoft Excel.app' {path}
On my Mac, I was able to replace the equivalent of /Users/username with ~. And so path = '~/name.xlsx' also worked from in a Jupyter to trigger launching of the Excel application with the specified spreadsheet file open.
You were using the full path, and so I kept that in my examples.
You can also change the working directory inside the notebook using the magic command %cd. If you change the current working directory to the directory where the file to open resides, you can and then shorten the 'path' to just be the name of the file.
Have you tried this:
import os
os.system("start EXCEL.EXE path")
If that dosn't work try yo use this Use Python to launch Excel file
as help :)

Running python file through VS Code terminal

My question is I am unable to run a python file in VS Code terminal unless I specify the full path.
Whereas, when I see any python tutorial, only python filename is entered and it works.
Can somebody pls help me with this issue?
The files need to be in the same folder where you are operating / or have the other Python files.
You can use two ways for this:
first:
import os
pwd = os.getcwd()
# again, make sure you file is in the same directory.
the_file = (pwd + "\\filename.xlsx")
or Secondly,
#the below you can use wherever your file is and it will locate it.
# you specify the full path using pathlib.path and:
pathlib.path(directory here without the file itself).joinpath(file name here).

Import on shared object fails with ModuleNotFound only when script is executed from command line

ran into a weird problem where there is a shared-object import error only when I run the script from command line. It succeed if i run the script in the python console using exec(...)
I have a class that needs a shared object: foo.py:
import os
cur_dir = os.curdir()
os.chdir('/tmp/dir_with_shared_object/')
import shared_object_class
os.chdir(cur_dir)
class Useful:
... # use the shared object import
Then there is a script like this:
from foo import Useful
If I enter python console and run:
exec(open('script.py').read())
Everything works fine.
If I run this on command line:
python script.py
I will get
ModuleNotFoundError: No module named 'shared_object_class'
The python is the same. It is 3.7.3, GCC 7.3.0 Anaconda. Anyone knows what is causing this discrepancy in behavior for shared object import?
A standard way of importing from a custom directory would be to include it in the PYTHONPATH environmental variable, with export PYTHONPATH=/tmp/dir_with_shared_object/.
update
It could also be done dynamically with
import sys
p = '/tmp/dir_with_shared_object/'
sys.path.insert(0, p)
PS
I think I have an explanation for why OP's original code didn't work. According to this python reference page, the import system searches, inter alia, in "[t]he directory containing the input script (or the current directory when no file is specified)." So the behavior in the REPL loop is different from how it is when running a script. Apparently the current directory is evaluated each time an import statement is encountered, while the directory containing the input script doesn't change.

How to run py file that uses txt file in Jupyter Notebook

I'm trying to run a python file through Jupyter Notebook. The first part of this file asks the user to import the data (.txt) file. Running it on a python IDE works well, however I want to make it work in Jupyter Notebook. I tried uploading the data beforehand, as seen in the image below.
It still doesn't detect the file nor reads the data. Any ideas on what else to try?
your jupyter has a directory where the notebooks are saved. If the file is not inside the notebook working directory (or a subtree of it) you can't access it, because it not exists. Your path C:\User\Downloads is obvisouly not this directory. Set your download directory to a jupyter directory, or move your text file. Check in jupyter what your working directory is.
Path errors are common in python. Especially if you often have to switch the programming language and then, few weeks later, back to python. I started to make me a helper method that I use in general in every new notebook and python application, to make not path errors. Maybe it helps you with correct working directory paths in the future.
import os
import sys
from pathlib import Path
def SetWorkingDir(abs_working_dir_path):
Debug = True
try:
if Debug:
print(f"[SetWorkingDir] Initial working directory: {os.getcwd()}", flush=True)
RootDir = False
if RootDir:
root_dir = os.path.dirname(os.path.realpath(__file__))
if Debug:
print(f"[SetWorkingDir] Local dir: {root_dir}", flush=True)
if (os.getcwd() != abs_working_dir_path):
os.chdir(abs_working_dir_path)
if Debug:
print(f"[SetWorkingDir] Working directory changed to: {os.getcwd()}", flush=True)
else:
if Debug:
print(f"[SetWorkingDir] Working directory unchanged at: {os.getcwd()}", flush=True)
return os.getcwd()
except Exception as ex:
print(f"\n[SetWorkingDir] Exception:"
f"\n{'Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(ex).__name__, ex}\n", flush=True)
And then all further paths relative to youur so set working directory (to make sure that your code also runs on other systems thans yours. Also use os.sep instead of \ as path seperator.)
best greetings

How to run an existing function from Jupyter notebook

I am using Jupyter notebook. In the same folder in which the notebook is running, I have a function f defined as
def f(x):
return x**2
I have saved this function as f.py in the same folder. Now I want to call this function in the notebook that is running. How do I do that? If the function was typed into the notebook, I could have just typed
f(4)
Try the load magic;
%load f.py
That automatically loads the in the entire contents of file so that you can edit it in a cell.
from f import f
Is another option.
If neither one of those work for you could try adding your notebook's directory to the the system path by running this block as a cell before trying to call your function;
import os
import sys
nb_dir = os.path.split(os.getcwd())[0]
if nb_dir not in sys.path:
sys.path.append(nb_dir)
%run f.py
load magic was just copying the whole file into a cell, which was not what i need. Neither did the importing worked for me. was throwing some weird errors. So i ended up using the run magic.

Categories