Guido's Gorgeous Lasagna problem

Hi, I’m new to Exercism. I attempted the Guido’s Gorgeous Lasagna problem, and I thought I did well. My solution is pretty much exactly the same as the author, BethanyG, but I can’t get my program to pass the test. The only real differences from the author’s solution and mine is that I used 3 single quotes for the docstrings instead of double quotes, and I put in some input statements and print statements to make it interactive. I don’t think that’s why it isn’t passing the tests. I got the following error messages:

"
Ran 1 test in 0.003s
Error
Traceback (most recent call last):
File “C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1264.0_x64__qbz5n2kfra8p0\Lib\unittest\loader.py”, line 34, in testFailure
raise self._exception
ImportError: Failed to import test module: lasagna_test
Traceback (most recent call last):
File “C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1264.0_x64__qbz5n2kfra8p0\Lib\unittest\loader.py”, line 154, in loadTestsFromName
module = import(module_name)
^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\monop\Exercism\python\guidos-gorgeous-lasagna\lasagna_test.py”, line 2, in
import pytest
ModuleNotFoundError: No module named ‘pytest’

FAILED (errors=1)

Process finished with exit code 1
"

I did install pytest using pip in the Windows command prompt, as well as pytest-cache and pytest-subtests. I get the impression from these error messages that my program isn’t passing not because of the content of the program itself but because I didn’t download something or I didn’t set up my environment correctly or I didn’t import something. What do I have to do to fix this?

That sounds like Python can’t find the pytest module.

How did you run the tests? There’s a small chance you may need to restart your shell after installing pytest to ensure the environment picks up any needed paths.

Hi @KrashOverryder :wave:

Chiming in here to add that using input() will toss an error once you load your solution up to the website. All our tooling runs in docker containers that do not have command lines, stdin, nor any connection to the internet.

Since input() is used to receive user input from the command line/stdin, it will hang the container and fail the tests. I’d recommend you remove all input() (and print() lines – to be tidy) before uploading to the website.

As for the pytest error you are receiving:

  1. Are you sure you made your installation of pytest global? That error being tossed means that your Python installation can’t find pytest to import it to run tests. If you installed pytest in a venv, in WSL, or another place that’s not on your path, the Python interpreter is going to have trouble finding it.
  2. Did you try running tests with python -m pytest -o markers=task lasagna_test.py ? That would attempt to find the pytest linked to your current flavor of python.
1 Like

Thanks BethanyG and IsaacG! I will remove the input and print statements before resubmitting.

I basically ran it in PyCharm. Even though I installed pytest with pip, I just noticed that if I try to import pytest in my Lasagna program in Pycharm it says I need to install pytest, so I guess that means it’s a separate installation to install it into PyCharm or it’s not global like BethanyG said. If I click on the little menu that pops up there’s an option to ‘Install package pytest’, then if I click on that, a box pops up that says,

“Administrator Privileges Required: Installing packages into ‘Python 3.11’ requires administrator privileges. Configure a per-project virtual environment as your project interpreter to avoid installing packages to a protected area of the file system.”

and it has options to either ‘Configure’, 'Install Anyway, or ‘Cancel’. If I click ‘Configure’ it gives two more options for Python Interpreter: ‘Python 3.11 (venv)’ or ‘Python 3.11’. I noticed on the pytest website it says,

“For development, we recommend you use venv for virtual environments and pip for installing your application and any dependencies, as well as the pytest package itself. This ensures your code and dependencies are isolated from your system Python installation.”

so does that mean I should choose the (venv) installation in PyCharm? What should I do?

And I haven’t yet tried the tests BethanyG recommended. So I would just type “-m pytest -o markers=task lasagna_test.py” into the command prompt in the same directory that lasagna_test.py is in?

I happen to use PyCharm – but I don’t use it to create my virtual envs or run my tests. I typically use the the command line for those things. I also don’t really like how PyCharm does things testing-wise.

Here is a venv primer from Real Python that walks through the whys and hows of virtual environments on Windows and other platforms. I think I’d recommend getting comfortable with it before you go configuring PyCharm. PyCharm has a lot of bells and whistles and complexities that aren’t very friendly or clear – so being able to do a venv without that interface will be an advantage. You can even run tests in the venv without firing up PyCharm at all.

Once you get comfortable with the venv situation (and install everything you need for exercism), you can then “hook up” Pycharm by telling it which environment you will be using, and which interpreter. I found these directions here from Jetbrains – but they will vary based on the version of PyCharm you have. Once things are hooked up, you can run tests directly from the PyCharm interface. Be warned tho: Jetbrains swapped the order of expected vs actual in their testing interface, so all of the exercism tests show up “backwards”. Very annoying!

The command I typed in assumes that you are using the command line and that you have your venv activated. Once you do that, you can navigate to the exercism exercise folder and run Python -m pytest -o markers=task lasagna_test.py, and pytest should pick up the tests and run them. It will then output results to the terminal.

You can also run the test via the PyCharm terminal – but you will need to activate your venv from PyCharm, and then use the PyCharm terminal to navigate to the exercise folder and run the tests.

2 Likes

Thank you, BethanyG! I’m not just new to Exercism; I’m new to programming, so I don’t know what “expected vs actual” means. But if you think it behooves me to not use PyCharm, I will try to set up venv by reading that primer. Using an IDE is likely too advanced for me at this point.

The way unit tests work is they call a function with specific inputs. When working correctly, they expect the function to return a specific “expected” value. What they get back from the function is the “actual” value. If the expected value and actual value are the same, the function is working as intended. If the values differ, the function is not working correctly.

def test_behavior(self):
    # Call the function with some test data.
    data = "some test data"
    # If working correctly, the function should return this:
    expected = "This is what the function ought to return."
    # Call the function and see what it actually returns
    actual = function(data)
    # Check if the expected and actual match:
    self.assertEqual(expected, actual)
2 Likes

Apologies for the really long response!

The TL;DR: learn about venvs via the article, then pick a code editor or IDE to hook those envs to. Don’t try to do both steps at the same time.

Its less about an IDE being “too advanced”, and more about certain setups being confusing if you don’t know how the underlying bits fit together. :smile:

Understanding the purpose of “project isolation” or “dependency isolation” – using something like a venv, conda env, docker container, or dev container to organize Python versions, project libraries, and related tooling can really help because you can then hook those environments up to any sort of code editor or IDE and know that everything is going to work the way you expect it to.

Isolating projects can also give you the freedom to quickly try different Python/library/tooling combinations and not worry that, for example, your website project is going to conflict with your data science experiments.

So yes - I am a heavy proponent of learning when to use or not use a venv or docker container. :smile: It makes things easier in the long run.

And I didn’t mean to scare you off of PyCharm. There are many developers that swear by PyCharm (as I said, I use it quite a bit). But there are also many alternatives to PyCharm that are in wide use (sublime text, vscode, jupyterlabs, vim, emacs, spyder, rstudio, Visual Studio, eclipse, and more).

It’s a personal matter of figuring out what works best for you and how you like to code. But that takes a bit of time, and also a bit of work as you explore different setups and different programs.

So - all that being said, I think you are fine using PyCharm. I just don’t think you should use the PyCharm interface to learn about virtual environments. :wink: You should learn about virtual environments from either that tutorial, or another one that focuses just on project isolation.

And there are text editors/IDEs that are more straightforward to configure than PyCharm. Many students like vscode for its ease of setup. The Python track docs have a small rundown of other editors beyond PyCharm and VSCode that you can try out. You could also start a thread here to ask folx what their preferred Python setup is to gather some ideas.

Just let IsaacG or myself or others know if you have questions or issues - we are all happy to help!

2 Likes

Do I always need to activate the venv from the command line or can I just click the ‘Activate’ icon in the venv’s ‘Scripts’ folder in Windows Explorer?

And do I need to activate the venv every time I turn on my computer to continue using it, or does it remain in that venv until you deactivate it or use a different venv?

Can you have multiple different venv-ies activated at the same time? If you can only have one activated at a time, does activating one automatically deactivate the one which was previously activated, or do you need to deactivate one before activating the next?

When would be a situation in which you would want to be in global Python?

1 Like

My my my are you full of questions! :smile:

I :heart: it!!
Be warned tho: this is a long post.

I’ll have to fire up my windows image to find out! I think this is dependent on how your path is set, and other factors. Gonna plead the 5th and then get back to you after I run some tests on my Windows 10 image - if that’s OK?

I always use the terminal (both inside PyCharm or other IDES and from my Mac iTerm).

 

At its root, a venv is a directory and a series of files and pointers to files. So if you’ve activated a venv, it will put the PATH to that venvs Python interpreter first, so that that version of Python (and all the environments related libraries) will be found first. That path config is going to stay that way until it’s overwritten by a deactivation script…or another event that resets your PATH. Turning off your computer is such an event. However, putting the computer to sleep doesn’t do that (or shouldn’t ). So if you reboot, you have to reactivate. But if you sleep and then resume, you don’t. ’

 

This is another thing that I want to test on my Windows 10 image before I am absolutely sure. cmd and powershell might very well work differently from my Mac’s iTerm when it comes to PATHs. But from my Mac/linux perspective:

If you’ve activated a venv in a terminal window, the PATH for Python only changes for that terminal session, so it’s possible to open a different terminal session and activate a different venv. I’ve had up to 5 different venvs in 5 different terminal windows running at the same time. Of course my 'puter did eventually get sad when these all consumed too much memory – but that’s a different problem. :smile:

As for auto-decativation? Um, no. There are programs that help manage things for you, but as far as I know (and I don’t know everything!!) there aren’t any that automagically handle activation/deactivation for you. Poetry might - I’ve never used it. virtualenvwrapper comes close with the WORKON command, but you still have to be explicit, IIRC. Anaconda Navigator has a somewhat more friendly interface for venvs, but the Anaconda distro is likely too much for someone starting out.

…but you should look around and give different options a go on your computer. :wink:

 

So … I never really use my “global” install. Until this year, it was actually at Python 3.7. :laughing: But I have a rather “creative” setup. This question might better be answered in a separate thread where you ask other folx. :smile:

Other devs may differ in this habit – if they are writing a quick script they don’t need other libraries for, or they have a dev env they know won’t be changing all that much, or they have one project they mostly work on without any other collaborators.

But I know myself: I can’t be trusted not to go down a rabbit hole and install stuff that will then pollute my global install and that I will immediately forget about. I also just have a wide variety of projects with dependancies that often conflict. Finally, I do a lot of troubleshooting/testing with different versions of Python, and there is no sane way to manage that short of venvs or docker or both.

Alright. Thanks for reading to the bottom. :smile: Apologies for … rattling on. :wink:

2 Likes

Apologies unnecessary! Extensive explanations are the only way I’m going to learn. My apologies for bothering you with my rudimentary questions. I think that for now at least I understand what I need to understand about venvies.

2 Likes