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!
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
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:
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.
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.
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.
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.