0

I am trying the first example from here.

But for some reason I get the message: Microsoft Visual Studio The project cannot be launched because of the startup file 'C:\Users\itay8\source\repos\fibo\module1.py was not found.

the 'fibo.py' file and the 'import_from.py' that I am trying to import to are in the same folder.

the version of python that I am using is 3.80

the code in fibo.py file:

Fibonacci numbers module

def fib(n):    # write Fibonacci series up to n
    a, b = 0, 1
    while a < n:
        print(a, end=' ')
        a, b = b, a+b
    print()

the code in import_from_fibo.py:

import fibo
fibo.fib(10)

now to the current problem is that it can't find the function. the output: Exception unhandled module 'fibo' has no attribute 'fib'

Itay Segev
  • 27
  • 5
  • Does this answer your question? [Call a function from another file in Python](https://stackoverflow.com/questions/20309456/call-a-function-from-another-file-in-python) – Karl Anka Nov 19 '19 at 13:51
  • Can you please edit this post to include the full, current contents of both files? Also, please note the version of Python you are using (try `py --version`). – Alexander Martin Nov 19 '19 at 15:51
  • Are you totally sure that the files are in the same directory? Check using Explorer or the `dir` command. – Alexander Martin Nov 20 '19 at 13:24

3 Answers3

0

Opening a module does not make its content directly accessible.

When you type import fibo, you just tell Python that you will use it later. To access the fib function you have at least two possibilities:

  1. Open the module, and access fib:
import fibo

fibo.fib(3)
  1. Import only fib and use it directly:
from fibo import fib

fib(3)

You can find more details on the page you linked https://docs.python.org/3/tutorial/modules.html

Arindam Roychowdhury
  • 5,927
  • 5
  • 55
  • 63
guillaume.deslandes
  • 1,191
  • 9
  • 12
0

Instead of using Visual Studio, try using the command line Python interpreter for your early practice. You can go back to Visual Studio later when you start a bigger project, but for now, it should work to just type:

> cd <wherever your project is>
> py import_from_fibo.py

This should run the contents of import_from_fibo.py.

I also note that its current contents do not call the function fib; try adding

print(fibo.fib(5))

or so to the bottom of import_from_fibo.py.

Alexander Martin
  • 443
  • 3
  • 12
  • C:\Users\itay8\source\repos\fibo>py import_from_fibo.py Traceback (most recent call last): File "import_from_fibo.py", line 2, in fibo.fib(10) NameError: name 'fibo' is not defined. I tried that :\. any idea why it cant find the fibo file? – Itay Segev Nov 19 '19 at 15:45
  • Have you ensured that `import_from_fibo.py` file contains both the import and the call? I tried to recreate your situation, but it appears to work for me... – Alexander Martin Nov 19 '19 at 15:51
  • they are in the same solution so I think so. – Itay Segev Nov 19 '19 at 16:08
  • Use `dir` or File Explorer to make absolutely sure the two are in the same directory, and that both have the filenames you expect. I would recommend not using Visual Studio for this until you're more well-versed in Python. – Alexander Martin Nov 19 '19 at 16:17
-1

add an empty file naming __init__.py to make it a module.

user1235183
  • 3,002
  • 1
  • 27
  • 66