0

To structure my python project, I thought of dividing the code into different files.

I found a huge guide about importing here and example 1 seemed to fit my needs.

I therefore create a file part.py with this code:

print("Hello world")

def myFunction():
    print("This is my function!")

Then I created destination.py in der same folder:

import part
# from part import *

myFunction()

As I expected that import part would run the whole code of part.py, my aticipated outcome was

Hello world
This is my function!

However, I got

ModuleNotFoundError                       Traceback (most recent call last)
 in ()
----> 1 import part
      2 # from part import *
      3 
      4 myFunction()

ModuleNotFoundError: No module named 'part'

Using # from part import * instead of import part leads to the same error.

What am I doing wrong?


EDIT: @hiro protagonist: I placed an empty __init__.py file into the same dictionary. After restarting the kernel, I get (notice the hello world) this when runing all the code in destination.py:

Hello world
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
 in ()
      2 # from part import *
      3 
----> 4 myFunction()

NameError: name 'myFunction' is not defined

After runing all the code in destination.py again, I only get

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
 in ()
      2 # from part import *
      3 
----> 4 myFunction()

NameError: name 'myFunction' is not defined

If I now, as a third action, run all the code in part.py, I get

Hello world

Then I can run all the code in destination.py to get

This is my function!

I would prefer it if I could only run destination.py over and over again with the same result.

@Nullman: part.pyis file, but import .part throws

 File "", line 1
    import .part
           ^
SyntaxError: invalid syntax

@Jeyekomon: I'm using Visual Studio Code on Windows 10 with Python 3.7.0.

@9769953: I run the code directly in Visual Studio Code

Julian
  • 591
  • 5
  • 14
  • do you have a `__init__.py` file (may be empty) in the same directory as `part.py`? is this directory in the `PYTHONPATH`? – hiro protagonist Apr 30 '19 at 08:11
  • create an empty `__init__.py` file in same folder – Amit Nanaware Apr 30 '19 at 08:11
  • is `part` a file? if its just a file and you are running from the same dir, you will need to use `import .part`, notice the dot. if part is a module, make sure there is an `__init__.py` file in the same dir and that python knows where it is – Nullman Apr 30 '19 at 08:12
  • 2
    Your code works fine for me. What OS are you using? What version of Python are you using? What command did you use to run your code? The error is formatted differently than I am used to. – Jeyekomon Apr 30 '19 at 08:21
  • How do you run your code? Do you use `python destination.py`? – 9769953 Apr 30 '19 at 08:22
  • I added some more information according to your suggestions and questions. – Julian Apr 30 '19 at 08:37
  • Don't you mean to use `part.myFunction()` when importing like `import part`? – funie200 Apr 30 '19 at 08:49
  • your code and `part` arent in the same directory, right? it would seem your python does know WHERE part is, is the path to part on your PATH? did you remember to put the `__init__.py`(even an empty one) where part is? – Nullman Apr 30 '19 at 08:56

2 Answers2

0

The module importing issue seems to be fixed now. Although the __init__.py file is usually no longer needed in Python 3, the Visual Studio Code, you're using, seems to still require it.

Rest of the questions is related to working with modules. Learn basics of creating and using modules from the official Python tutorial. The module part should contain functions that you can call in your main file destination:

destination.py:

import part

part.say_hello()
part.myFunction()
part.say_hello()

part.py:

def say_hello():
    print("Hello world")

def myFunction():
    print("This is my function!")
Jeyekomon
  • 2,878
  • 2
  • 27
  • 37
-1

Change your destination.py to:

from part import *
myFunction()

That works for me

  • The OP deals with `ModuleNotFoundError`. Your proposed solution fixes `NameError` so it does not fix OP's issue. – Jeyekomon Apr 30 '19 at 08:27