32

A very basic questions on importing modules created locally.

I am unable to import a locally created module. The module exists in the current working directory

enter image description here

enter image description here

Am i missing something?

8
  • 1
    If your module is in a folder you'll have to do from foldername.myModule import Person Commented Apr 26, 2018 at 13:46
  • The current working directory of the notebook server is the base path where you started the server. How about -- import sys sys.path.append("path to your module") Commented Apr 26, 2018 at 13:48
  • It is not in a separate folder, both the module (to be imported) and the notebook where I am calling or importing the module are on the same path. Commented Apr 26, 2018 at 13:53
  • I have created an env. variable PYTHONPATH to set current working directory as the sys path. If I am not mistaked import sys sys.path.append("path to your module") would do the same. Commented Apr 26, 2018 at 13:57
  • 2
    I was just facing this issue after uploading a .py file to import and none of the zillion solutions on SO worked. So I went back to basics and just restarted the kernel. lo and behold it now works as expected with a simple import statement. Commented Aug 17, 2018 at 1:23

4 Answers 4

7

I'm finding local library names must begin with a capital letter. If I keep all my local files in a folder called Code, I can import them; if it's called code, I cannot. (The names of subfolders and subfiles don't seem to suffer that restriction.)

This appears to be a Jupyter restriction, not a Python one -- from the command-line Python repl I can import whatever local .py file I want.

Sign up to request clarification or add additional context in comments.

2 Comments

man, I love you. I have been fighting against an import for hours. I wish I had found your answer hours ago.
I know the feeling. Today I spent half an hour writing a bug report, only to when filing it have the submission validator inform me similar issues already existed, and in fact it had been submitted twice before, and was already solved in the development branch ... Anyway happy to help.
6
%%capture
%run myModule.ipynb

You will get all functions/variables defined in myModule file. This will also overwrite variables of your current notebook but

from Mymodule import person

has also that effect.

1 Comment

Are you sure that running the following- %%capture %run myModule.ipynb would also import the variables? I don't think it will. It only imports the functions
5
import os 
#if you want to know current working dir
os.getcwd()
#if you want to change
os.chdir('G:/a-2017-master')
# if you want to list dir
os.listdir()

['.DS_Store', '.gitignore', 'cs109a_hw0.ipynb', 'hwassets', 'Labs', 'Lectures', 'Midterms', 'Module.py', 'Projects', 'README.md', 'Sections', 'pycache']

import os
import Module as m 
a = 10
b = 29
print(f"Addition of {a} and {b} : ",m.add(a,b))

Comments

0

Here is an example from the W3schools Tutorial to create module locally:

  1. In a conda environmnet keras, 'cookie.py' module is created and jupyter notebook is initiated in the same path. Then create a file named 'Importing_module_locally'.

(keras) ninjawarrior@ninjas-MBP cookiecutter % pwd
/Users/ninjawarrior/miniconda3/environments_files/pythonbasics/Python_Tutorial_w3schools/mymodules/cookiecutter

  1. Confirming both module and jupyter notebook file is on the same path.

(keras) ninjawarrior@ninjas-MBP cookiecutter % ls -lrt
total 16
-rw-r--r--  1 ninjawarrior  staff   46 Oct 12 12:47 cookie.py
drwxr-xr-x  3 ninjawarrior  staff   96 Oct 12 12:50 __pycache__
-rw-r--r--  1 ninjawarrior  staff  751 Oct 12 12:56 Importing_module_locally.ipynb

  1. Enter the below in cookie.py

def greeting(name):
  print("Hello, " + name)

`

  1. Enter the below in Import_module_locally

import mymodule                  

mymodule.greeting("Jonathan")

Result : Hello, Jonathan

Hope this helps !

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.