0

I am aware that this question has been asked before and I am trying one of the previously answered questions but it is still not working. I want to go through the route of converting the directories into packages but it doesn't seem to be working.

I need to import the RecipeManager class from the recipeProject.py file into my testExportRecipes.py file. I wanted to have a clean folder structure by keeping all the test files in their own folder.

The commented block of code at the start is a solution that works but I prefer the other way and want to learn why it's not working in my case.

The last thing I tried was running the file from the parent directory of my project file according to the solution over here

My current folder structure

testExportRecipes.py

# import os, sys
# CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
# sys.path.append(os.path.dirname(CURRENT_DIR))

import unittest

from NewProjectJustDropped.recipePackage.recipeProject import RecipeManager


class TestRecipeManagement(unittest.TestCase):

    def test_exported_JSON_file(self):
        rm = RecipeManager()

Snippet of recipeProject.py

import json
import os

class RecipeManager:
    def __init__(self):
        self.data = [
            {
                "id": 0,
                "recipeName": "McBurger",
                "recipeAuthor": "Sam",
                "prepTime": 10,
                "cookTime": 12,
                "servingSize": 1,
                "ingredients": [
                    {"ingredientName": "bun", "quantity": 1, "measurement": "unit"},
                    {"ingredientName": "secretPatty",
                     "quantity": 1, "measurement": "unit"},
                    {"ingredientName": "specialMayo",
                     "quantity": 10, "measurement": "grams"},
                    {"ingredientName": "specialSauce",
                     "quantity": 20, "measurement": "grams"},
                    {"ingredientName": "lettuce",
                     "quantity": 8, "measurement": "grams"},
                    {"ingredientName": "tomato",
                     "quantity": 8, "measurement": "grams"}
                ],
                "instructions": {
                    "1": "Assemble the bun and the secret patty.",
                    "2": "Spread special mayo and special sauce on the bun.",
                    "3": "Add lettuce and tomato on top.",
                    "4": "Cook the assembled burger for 12 minutes."
                }
            }
        ]

    def viewRecipe(self):  
        # this method should print all recipes to the screen.
        pass
Samson
  • 35
  • 5

1 Answers1

1

The problem you are having here is the relative import. If you run the test.py from inside a folder, that folder will be the root path. If you move that test.py to the parent folder, then you'll be able to load the class with the code you already have from NewProjectJustDropped.recipePackage.recipeProject import RecipeManager

One solution is to move the test to the parent root of the project, which some developers recomend, and you can gitignore every single 'test*' file, naming them all test_whatever.py.

Another solution would be to insert the python path to that module folder. But this is not the ideal solution. sys.path.insert(1, '/path/to/app/folder') or sys.path.append('/path/to/app/folder')

Be careful when running the test.py. That path will be relative if it's inside a test/folder.

If you are having problems to understand this, try to run the test.py from the terminal.

$ path/to/project.py python test_folder/test.py

In this case you need to specify the test_folder where you place your tests.py. In case you have the test.py in the root of the project...

$ path/to/project.py python test.py

This will be the command.

  • I am a little confused about where you suggested to move my test file. Do you mean it should be in NewProjectJustDropped or one level above it? Also, is there a solution where I can keep my current folder structure? Since you mentioned the "recommendation", is it better if I keep all my tests in a folder one level above the NewProjectJustDropped folder? – Samson Jun 07 '23 at 11:26
  • The root directory is always the 'base' of the project. So in this case, the root directory is 'NewProjectJustDropped'. If you place your test where your recipes.json is, you'll be able to import without problems the class with the code you have right now. To say so, the problem you have right now is that you are trying to import from /tests_folder/ something in a route that does not exist. When you write the import in python, you need to think of it as the folder structure from where the file is right now, and inside the test_folder there is no NewProjectJustDropped folder. – Jose M. González Jun 07 '23 at 11:39
  • In resume, you are trying to import a folder that the /test_folder/ does not have, since the test.py file import path is from where it is placed right now. The best solution in your case is to create relative imports, going up one level is just adding a dot in front of the folder structure 'from .recpicePackage.recipeproject import RecipeManager' or inserting in your test.py the path to the NewProjectJustDropped folder with sys.path.append('/path/to/NewProjectJustDropped') – Jose M. González Jun 07 '23 at 11:44
  • Anyways, I think you need to take a look into folder structurs and try to navigate them a bit from your terminal. This maybe helps you to clear some doubts about it. My suggestion right now is to move all your tests to the root of the project. Be careful to not use the rootname to import something 'from NewProjectJustDropped...' it's just wrong, since your test.py is going to be in the root, you just need to mention the folders... 'from folder.subfolder.package.py import RecipeClass' – Jose M. González Jun 07 '23 at 11:51