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.py
is 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