1

I've been trying to look for the solution to my specific error, but have had no success.

So, in the Python3 environment in my terminal, I type the following for 'problems1.py:

>>> import problems1.py

problems1.py:

import math

print("Please enter a number 'x' ")
x = input()
x = float(x)

print("Please enter a number 'y' ")
y = input()
y = float(y)

exp = x ** y
lgrm = math.log2(x)

print(f"The number 'x' raised to the power of 'y' is {exp}, and ...")
print(f"The log(base 2) of x is {lgrm}")

I am able to input the requested numbers and output the print statements only once, however, at the end of the outputs, the following error is shown:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'problems1.py'; 'problems1' is not a package

Any subsequent attempts to import problems1.py only returns the error.

9
  • 1
    Try just import problems1 Commented Mar 12, 2021 at 18:26
  • Can you show your dir structure? Are you using anaconda? You may want to look into python's basic features, especially about its module structure and __init__py files. We can answer all of your questions, but I'm pretty sure you'd profit a lot more from slowly but surely reading the basic doc/introduction to Python. You look smart and willing. ;) Commented Mar 12, 2021 at 18:32
  • @J.F.Ramirez Ok I am confused when you import something you are not supposed to get any output. You are just including the file or package in your code. You still have to call it to get it to do anything. Commented Mar 12, 2021 at 18:50
  • @DwightFoster The first time I type 'import problems1' and hit return, I am able to input the requested variables and output their math results. The second time, or any other time, I type 'import problems 1' and hit return, nothing happens. Commented Mar 12, 2021 at 19:02
  • 1
    @J.F. Ramirez Ok so now I am kind of understanding what you want to do. I think you are trying to run the code correct? If so you would want to do something like python problems1.py or python3 problems.py depending on the terminal. That would run the code importing is not used for a scenario like this. Commented Mar 12, 2021 at 19:16

1 Answer 1

2

I think I have an answer for your question.

I'll simplify your file problems1.py to make this explanation easier to write down.

Let's assume that your file problems.py has the following piece of code inside of it:

#contents of the file: problems.py
firstname = input("First name: ")
lastname = input("Last name: ")
print(f"Your full name is: {firstname} {lastname}")

If we launch that file, then we get the following output:

First name: Kevin
Last name: Durant
Your full name is: Kevin Durant

Basically what happens when you issue an import is that the Python interpreter opens up the file that you import, and goes through it line-by-line, and tries to create the namespace for the file, and make some optimizations of the code.

As such, when the interpreter reaches the line firstname = input("First name: ") it will have to execute the code to the right of the equals-sign, to map the value to the namespace firstname.

The interpreter will read through the whole file, to be sure that it didn't miss any changes to the whole namespace before it will return to execute any other command after the import call.

So let's start an interpreter and issue import problems:

>>> import problems
First name: Kevin
Last name: Durant
Your full name is: Kevin Durant
>>>
>>> problems.firstname
'Kevin'
>>> problems.lastname 
'Durant'

See that when we issued the import, it executed the code inside of it. You can also see that we can access firstname and lastname from problems by accessing its attributes using problems.firstname and problems.lastname.

So, let's look at what happens when you issue import problems.firstname:

>>> import problems.firstname
First name: Kevin
Last name: Durant
Your full name is: Kevin Durant
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'problems.firstname'; 'problems' is not a package

What is happening here, is that when you issue a relative (dot separated) import, the Python interpreter assumes that what you're importing is a sub-file of the problems package.

Because it cannot find a package there, it throws an error, and crashes the code.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.