-1

The relevant folders in my django project are here.

-config (folder)
--settings (folder)
---common.py
---production.py

-core (folder)
--management (folder)
---commands (folder)
---my_command.py

I have a variable called MYVAR. If I put it in common.py and import it into my_command.py like so, it works.

from django.conf import settings

class Command(BaseCommand):
    def handle(self, *args, **options):
      test = settings.MYVAR

But if I put MYVAR in production.py and run the same code snippet, it can't find the variable.

What's going on here? common.py and production.py are in the same folder (settings). Why is it not importing from production.py?

sraval
  • 9
  • 3
  • I don't understand why people down vote questions without leaving a comment... – kpie Apr 09 '16 at 01:35
  • as you can see, you are not importing `common.py` or `production.py` from your code. What you are importing is django conf's settings (which of course is the right thing to do). Now all you need to do is to ensure that you are providing the right settings (i.e. production.py) when invoking the management command. – zEro Apr 09 '16 at 04:29

1 Answers1

0

See my answer on here I can't seem to get --py-files on Spark to work

which links here Import a module from a relative path

import os, sys, inspect
 # realpath() will make your script run, even if you symlink it :)
 cmd_folder = os.path.realpath(os.path.abspath(os.path.split(inspect.getfile( inspect.currentframe() ))[0]))
 if cmd_folder not in sys.path:
     sys.path.insert(0, cmd_folder)

 # use this if you want to include modules from a subfolder
 cmd_subfolder = os.path.realpath(os.path.abspath(os.path.join(os.path.split(inspect.getfile( inspect.currentframe() ))[0],"subfolder")))
 if cmd_subfolder not in sys.path:
     sys.path.insert(0, cmd_subfolder)

 # Info:
 # cmd_folder = os.path.dirname(os.path.abspath(__file__)) # DO NOT USE __file__ !!!
 # __file__ fails if script is called in different ways on Windows
 # __file__ fails if someone does os.chdir() before
 # sys.argv[0] also fails because it doesn't not always contains the path
Community
  • 1
  • 1
kpie
  • 9,588
  • 5
  • 28
  • 50