简体   繁体   中英

Python3 use imported module in function django

I upgrade from Djanjo 1.8 to 2.1

I noticed that when I try to use an imported module, the module is not found

Example #1

from var_dump import var_dump
from django.db import connection

def get_available_id():
    with connection.cursor() as cursor:
        query = """SELECT
        *
        FROM
        `stackoverflow`
        ;"""
        cursor.execute(query)
        row = cursor.fetchone()

    return row

var_dump(get_available_id())

This results in NameError: name 'connection' is not defined

Full dump

root@fbb4988d3d17:/var/www/api.domain.com# python manage.py shell < sql/import.py 
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/base.py", line 316, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/base.py", line 353, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/commands/shell.py", line 92, in handle
    exec(sys.stdin.read())
  File "<string>", line 27, in <module>
  File "<string>", line 8, in get_available_id
NameError: name 'connection' is not defined

How ever if I import the connection module in the function get_answers everything works

Example #2

from var_dump import var_dump

def get_available_id():
    from django.db import connection
    with connection.cursor() as cursor:
        query = """SELECT
        *
        FROM
        `stackoverflow`
        ;"""
        cursor.execute(query)
        row = cursor.fetchone()

    return row

var_dump(get_available_id())

This works.

What can I do to make sure example #1 works with global import.

I am running the code like this python manage.py shell < sql/import.py

The problem is solved by moving the class I am trying to run into a seperate file.

#class_a.py
import re
class A:
    def word_to_list(str):
        words = re.split("(\s+)", str)
        return words

a = A()
a.word_to_list('bob is your uncle')

running python manage.py shell < sql/class_a.py results in NameError: name 're' is not defined

The solution is

#class_a.py
import re
class A:
    def word_to_list(str):
        words = re.split("(\s+)", str)
        return words

then

#runner.py
from class_a import A
a = A()
a.word_to_list('bob is your uncle')

running python manage.py shell < sql/runner.py

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM