3

I have a file called hotel_helper.py from which I want to import a function called demo1, but I am unable to import it.

My hotel_helper.py file:

def demo1():
    print('\n\n trying to import this function ')

My other file:

from hotel.helpers.hotel_helper import demo1

demo1()

but I get:

ImportError: cannot import name 'demo1' from 'hotel.helpers.hotel_helper'

When I import using from hotel.helpers.hotel_helper import * instead of from hotel.helpers.hotel_helper import demo1 it works and the function gets called. I tried importing the whole file with from hotel.helpers import hotel_helper and then call the function with hotel_helper.demo1() and it works fine. I don't understand what's wrong in first method. I want to directly import function rather using * or importing the whole file.

Matt Hall
  • 7,614
  • 1
  • 23
  • 36
Shubham Devgan
  • 609
  • 6
  • 18
  • What does the directory tree look like (where are these files?), and what's in the `__init__.py` file? – Matt Hall Sep 26 '19 at 13:04
  • which __init__.py file are you talking about please be specific @kwinkunks – Shubham Devgan Sep 26 '19 at 13:13
  • @ShubhamDevgan to work with python module as package you need to create s `__init__` file inside that. That's what @kwinkunks is asking – Saleem Ali Sep 26 '19 at 13:17
  • @beer44 why would it need an ```__init__``` ,I'm just importing the function it works without an ```__init__``` file , I don't understand the need of creating it, I have same working for another file it works for it. – Shubham Devgan Sep 26 '19 at 13:23
  • @ShubhamDevgan, by using `__init__.py` it made easy to import the module without knowing the exact location of your module. because python include this in its namespace. ref https://docs.python.org/3/reference/import.html#namespace-packages – Saleem Ali Sep 26 '19 at 13:34
  • One possibility is to not have the file you want to import, inside your directory. For instance, if you are using VS code, your problem will be solved by opening the root of your code in VS code explorer. – M.Hossein Rahimi Nov 23 '21 at 21:38

6 Answers6

1

If you filename is hotel_helper.py you have to options how to import demo1:

You can import the whole module hotel_helper as and then call your func:

import hotel_helper as hh
hh.demo1()

You can import only function demo1 from module as:

from hote_helpers import demo1
demo1()
Porada Kev
  • 503
  • 11
  • 24
0

From your fileName import your function

 from hotel.helpers import demo1


    demo1()
Rebai Ahmed
  • 1,509
  • 1
  • 14
  • 21
0

You can import a py file with the following statement:

# Other import
import os
import sys

if './hotel' not in sys.path:
    sys.path.insert(0, './hotel')
from hotel import *

NOTE:
For IDE like PyCharm, you can specify the import path using the Project Structure setting tab (CTRL+ALT+S)

Helpful stack overflow questions [maybe off topic]:
What is the right way to create project structure in pycharm?

Manage import with PyCharm documentation:
https://www.jetbrains.com/help/pycharm/configuring-project-structure.html

This is probably a duplicate of: https://stackoverflow.com/posts/57944151/edit

alessiosavi
  • 2,753
  • 2
  • 19
  • 38
0

I created two files (defdemo.py and rundefdemo.py) from your posted 2 files and substituted 'defdemo' for 'hotel.helpers.hotel_helper' in the code. My 2 files are in my script directory for Python 3.7 on windows 10 and my script directory is in the python path file python37._pth. It worked.


defdemo.py


def demo1(): print('\n\n trying to import this function ')


rundefdemo.py


from defdemo import demo1

demo1()


output


trying to import this function

Joe McKenna
  • 135
  • 5
0

I was able to solve the issue, it was related to some imports I was making in my file, when I removed all the import statement in my hotel_helper.py ,the code started working as expected , Still I don't understand reason why the issue was occurring. anyway it works.

Shubham Devgan
  • 609
  • 6
  • 18
0

This ImportError can also arise when the function being imported is already defined somewhere else in the main script (i.e. calling script) or notebook, or when it is defined in a separate dependency (i.e. another module). This happens most often during development, when the developer forgets to comment out or delete the function definition in the body of a main file or nb after moving it to a module.

Make sure there are no other versions of the function in your development environment and dependencies.