How to Import Modules: ModuleNotFoundError: No module named 'hello'

Asked 2 years ago, Updated 2 years ago, 38 views

I'm studying as a self-taught programmer for Corey Alsov.
[Import other modules] on p.121 is failing.
I created a tstp folder on my desktop and created a hello.py file in that folder.I created another file called project.py in the same tstp file and tried to import it, but I got an error (ModuleNotFoundError: No module named 'hello').
I checked several times to find out how to create and import modules, but it failed.
I'm using Python 3·MacBook

Dear nekketsuuuu,Thank you for your reply!
However, it has not been resolved yet...

Possibility 1: Not in the same folder

Navigate to the directory where hello.py and project.py should be together at the terminal and ls
Please make sure that those two files exist.If you do not save the file properly or put it in a different location, it may not exist.

→ There are two tstp folders created on the desktop: hello.py and project.py.
How exactly can I find out if a file exists in ls?
Even if I look at various websites, I don't know which one to refer to...

Thank you for your cooperation.

python python3

2022-09-29 21:45

3 Answers

In the terminal, navigate to the directory where project.py and hello.py are supposed to be together, and check ls to make sure the two files are present.If you do not save the file properly or put it in a different location, it may not exist.

$cd tstp
$ ls
hello.py  project.py

(Two files should be displayed as shown above)

Verify that you typed the wrong file name hello.py or the module import part import hello.Full-width hello doesn't work.

When you run the program you created, you probably type it in like python project.py.At this time, make sure that cd is running in the directory where project.py is located.

$cd tstp
$ ls
hello.py  project.py
$ python project.py

When running import hello, Python looks for various paths to see if the module hello is missing.You can find your own hello.py without doing anything because it contains the current directory in this search path.If the current directory is not a directory with hello.py or project.py, it will not be found and will be ModuleNotFoundError.


2022-09-29 21:45

I am also studying Python 3 on my Mac after reading Self-Taught Programmer

How exactly can I find out if a file exists in ls?

Let me comment on this part.

Chapter 16 Bash (p.190~) in Self-Taught Programmer describes how to launch Terminal on your Mac, move directories (= folder), and check files in ls.
Why don't you read this chapter first?


2022-09-29 21:45

I'm also reading Self-taught Programmer and studying on Windows.
First, let me comment on the unique wording of this book.
No folder location is specified.
Also, when you read on, module1 displays >>Hello!.

You don't want to do this when the module is imported.

It is connected to .
I interpreted it as a series of trends.


2022-09-29 21:45

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.