More issues with declaring

Hi Team

I was using Anaconda and decided to ditch it in favour of just using Python / Idle.

I have ([PIP] reinstalled) all the modules I feel I will need, one of the foremost ones being [MatPlotLib], which installed/reintalled with zero issues, but!

I keep getting an error when I run the following code:

(from:Matplotlib Tutorial 19 - subplots - YouTube)

`import random

'import matplotlib.pyplot as plt from matplotlib import style`

`style.use(‘fivethirtyeight’)
fig = plt.figure()

def create_plots():
xs = [ ]
ys = [ ]

for i in range(10):
x = i
y = random.randrange(10)

xs.append(x)
ys.append(y)

return xs, ys`

ax1 = fig.add_subplot(211)
ax2 = fig.add_subplot(212)

x,y = create_plots()
ax1.plot(x,y)

x,y = create_plots()
ax2.plot(x,y)

plt.show

Now I get an error pointing to matplotlib:
================= RESTART: C:/Users/Me/pyProj/my_pyEnv/MyPy.py =================
Traceback (most recent call last):
File “C:/Users/Me/pyProj/my_pyEnv/MyPy.py”, line 2, in
import matplotlib.pyplot as plt
ModuleNotFoundError: No module named ‘matplotlib’

==========================================================================
Any assistance is appreciated

Cheers
Mark.

You have installed third party packages in a virtual environment, which is a good practice. Chances are that you are launching IDLE from the default environment, where it would not have access to those packages.

From your terminal, with your virtual environment activated, perhaps you can try to launch IDLE as follows:

python -m idlelib.idle

André

Hi André,

I think that errors like this one show that having beginners try to use

virtual environments is not a good practice. It’s complicated,

confusing, and unnecessary for somebody who doesn’t need all the extra

complexity of virtual environments.

Virtual environments are only useful if you need more than one

environment. For most beginners, learning Python is complicated enough

in a single environment without encouraging them to load complexity on

top of complexity with virtual environments.

Hi Steven,

I actually completely agree with your comments about not suggesting to beginners that they should use virtual environments.

As the OP mentioned that he followed a tutorial (which likely did suggest to use a virtual environment), I tried to put positive language in my answer (“good practice”) and suggest a solution that would work in their context, rather than implying that they are “doing it wrong” and should not use a virtual environment. I simply attempted to find the easiest/least disruptive solution for their current problem.

André

G’day Andre & Steven

Thank you both for your response.

As per the screenshot I uploaded, you will notice I am working inside a virual environment (my_pyEnv).

When I pip re-installed matplotlib (thinking I had done it incorrect the first time), it stated ‘Requirements already satisfied’ so I can only assume it is loaded correctly, which is why I was surprised when it showed the error.

I’m no stranger to coding (20 odd years in VBA [Excel & Access]) although, this is definitely new to me. Coding inside virtual environments is definitely something I did not have to worry about in VBA, so I can only go by what I see in the larger coding community and youtube tutorials, not ideal, but I figure it’s a start.

Once the educational institutes recommence here in Melbourne I plan to do part time studies, but! until then, I am playing with codes I find on youtube.

Essentially, I’m curious as to why I got the error, especially seeing I copyied the code from the youtube tutorial which ran without issue.

I was wondering if either of you could load the code and run it to see if it also returns an error. I’m confident I have mirrored what the tutorial had, so I am very interested in your results.

Thank you again for your time and patience.

Regards
Mark.

So!

It turns out I did have the syntax wrong, go me…

Declaring should have been:
import random
import matplotlib.pyplot as plt
from matplotlib import style

But!
I then got another error on the “Return” statement.
xs.append(x)
ys.append(y)
return xs, ys

I will keep plugging away at this one to get it right.

I did another tutorial and followed the coding example for a Door Game which is on FreeCodeCamp.org. I changed it slightly, but essentially the code is the same.

The following worked with zero issue:

Cheer
Mark

I noticed you wrote “pip install …”. You might want to read Why you should use `python -m pip`

André

Python defines code blocks by indentation. Without indenting the code properly in your posts, we cannot give you help with syntax errors and others.

Hi Andre

I will endeavour to use your command suggestion.

As per the attached screenshot, it again stated “Requirement already satisfied”.

Hi Andre

See attached screenshot:

Regards
Mark.

Python’s “return” can only use inside a function. As its names says, it “returns” some value. I suspect that you meant to write the following:

def create_plots():
    xs = []
    yx = []
    for i in range(10):
        x = i
        y = random.randrange(10)
        xs.append(x)
        ys.append(y)
    return xs, ys

Notice how the “for” statement is indented compared with “def” and how “return” is at the same level of indentation inside “def”, unlike what your wrote. As I mentioned in a previous email, Python defines code blocks using indentation.

The error you made, and could not figure out, illustrates a lack of knowledge of basic concepts in Python. Before you try to follow a topics-based tutorial (e.g. how to use matplotlib) that you found on the Internet, I strongly suggest that you follow first a Python specific tutorial. Given your stated experience with VBA, you can probably jump right in Python’s own tutorial (The Python Tutorial — Python 3.9.1 documentation) which is suitable for people who have some programming experience.