1

I'm trying to import a class in another python class in my pynecone project. It's giving me error.

import Constants ---> Giving error 

ModuleNotFoundError: No module named 'Constants'

Edit: Folder structure:

enter image description here

From helloworld.py, I'm trying to import Constants. How can this be done?

2
  • Does your import point to where the module is? It's hard to say what the problem is without the folder structure. Commented Jun 12, 2023 at 11:43
  • Where is Constants defined? What is your project structure? Generally, it's from module_name import ClassName Commented Jun 12, 2023 at 11:47

2 Answers 2

3

The way it's being imported within the library is

from pynecone import constants
1
  • This doesn't work. He has a folder that its name is "helloworld". If he wants to import Constants file in hellowold file, he has to import as from helloworld import Constants. Or he has to use the init.py file
    – AlexOAD
    Commented Dec 22, 2023 at 1:07
3

You can simple write:

from helloworld import Constants

Other options is use the __init__.py file to initialize the modules. You can write from helloworld import Constants as const (as is optional) and then, in the helloworld.py file write at the top from helloworld import const or relative import from . import const

Not the answer you're looking for? Browse other questions tagged or ask your own question.