-2

I have 2 files, 1 file with the classes and 1 file with the 'interface'

In the first file I have:

from second_file import *

class Catalog:

    def ListOfBooks(self):

        more_20 = input()
        # If press 1 starts showing from 20 until 40
        if more_20 == '1':
            for item in C[20:41:1]:
                print("ID:",item['ID'],"Title:",item['title'],"  Author: ", item['author'])

        elif more_20 == '2':
            return librarian_option()

test = Catalog()
test.ListOfBooks()

What I try to achieve is when the user presses 2, I want to go back to the function in my other file.

Second file:

def librarian_option():
.......

I don't want to use globals and I have read that the librarian_option() is in the scope of the second file and that's why I can't call it directly. I can't find a solution to it.

I get the following error:

NameError: name 'librarian_option' is not defined
Number70
  • 453
  • 1
  • 4
  • 16
  • 1
    Possible duplicate of [Call a function from another file in Python](https://stackoverflow.com/questions/20309456/call-a-function-from-another-file-in-python) – Devesh Kumar Singh Jun 11 '19 at 14:54
  • shouldn't this be ` def ListOfBooks():` be ` def ListOfBooks(self):` as this is inside class? how about `librarian_option`, is it inside some class? – Gaurang Shah Jun 11 '19 at 14:54
  • @9769953, it is located in the same file directory – Number70 Jun 11 '19 at 14:56
  • @Guarang Shah, true I forgot it.. – Number70 Jun 11 '19 at 14:56
  • @DeveshKumarSingh, I dont get an ImportError. Im getting a NameError. – Number70 Jun 11 '19 at 14:57
  • @Number70 how about my other question, is it a method (function inside a class) or a function ? – Gaurang Shah Jun 11 '19 at 14:57
  • @Guarang Shah, it is not inside a class, I only have 2 functions in the second file. – Number70 Jun 11 '19 at 14:58
  • I would suggest to fix the indentation in the question, right now it leads to a syntax error, also it works for me when I use `from second_file import *` assuming second_file.py contains `librarian_option` Also try using `from second_file import librarian_option` – Devesh Kumar Singh Jun 11 '19 at 15:00
  • make sure you have `__init__.py` file where these two files are located. – Gaurang Shah Jun 11 '19 at 15:01
  • @GaurangShah, yes I have an empty __init__.py file – Number70 Jun 11 '19 at 15:02
  • I created a `first_file.py` with your listed code (correcting for the indentation), and a `second_file.py` with a nearly empty function `def librarian_option: return 1`. With that, I can't reproduce your problem. It may just be a typo, and your names don't actual match. You can `print(locals())` before running `test = Catalog()` to see what you have actually imported. – 9769953 Jun 11 '19 at 15:04
  • @DeveshKumarSingh, strange that it isn't working for me.. – Number70 Jun 11 '19 at 15:04
  • No need for an `__init__.py` file: if these are two files, the first one being run directly within the current directory, the init file is not needed. – 9769953 Jun 11 '19 at 15:05
  • How, *precisely*, are you running the script? – 9769953 Jun 11 '19 at 15:05
  • @9769953, {'self': } – Number70 Jun 11 '19 at 15:07
  • You're printing `locals()` at the wrong level, I'm afraid. It has to be *outside* the class. – 9769953 Jun 11 '19 at 15:09
  • @9769953, what do you mean with outside the class? – Number70 Jun 11 '19 at 15:10
  • The output of your print function shows only `self`; for me, it shows a lot more. I can only produce your output if I print directly under `def ListOfBooks`, which is not directly above `test = Catalog()`. – 9769953 Jun 11 '19 at 15:19
  • @9769953, {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': , '__spec__': None, '__annotations__': {}, '__builtins__': , '__file__': 'C:\\Users\\x\\OneDrive\\Desktop\\Assignment29-5-2019\\PLS.py', 'json': , 'csv': } – Number70 Jun 11 '19 at 19:25
  • 'loadFile': , 'saveFile': , 'pop': True, 'ask': , 'customer_option': , 'librarian_option': } – Number70 Jun 11 '19 at 19:30
  • Interesting: it appears to be there, and even as a function. There's something funky going on here that we're not seeing. – 9769953 Jun 11 '19 at 19:42
  • @9769953, what do you suggest for me? – Number70 Jun 11 '19 at 20:05
  • I have provided a detailed comment in an answer, to help you improve your question, because there are likely some details missing. But a simple comment is not enough to clarify all that. See my "answer" for how to possibly proceed. – 9769953 Jun 12 '19 at 15:00

2 Answers2

0

Have you tried? It's just best practice to be explicit instead of using * wildcard

from second_file import librarian_option

Make sure the second_file is in the same directory.

Hieu Pham
  • 76
  • 3
0

Note: this is not an answer, but an example to help improve the question with more details.

You need to reduce your problem to the minimum amount of code (and actions) necessary to produce your problem. You also need provide how exactly you are running your script, and what version (of Python and your OS) you are using.

For example, I have created the following two scripts (named exactly as shown):

first_file.py:

from second_file import *

class Catalog:
    def ListOfBooks(self):
        return librarian_option()

test = Catalog()
a = test.ListOfBooks()
print(a)

second_file.py:

def librarian_option():
    return 1

These two files are located in the same, random, directory on my computer (MacOS). I run this as follows:

python3.7 first_file.py

and my output is

1

Hence, I can't reproduce your problem.

See if you can still produce your problem with such simplified scripts (i.e., no extra functions or classes, no __init__.py file, etc). You probably want to do this in a temporary directory elsewhere on your system.

If your problem goes away, slowly build back up to where it reappears again. By then, possibly, you've discovered the actual problem as well. If you then don't understand why this (last) change you made caused the problem, feel free to update your question with all the new information.

9769953
  • 10,344
  • 3
  • 26
  • 37