Django没有使用Python 3安装

时间:2016-06-07 13:48:21

标签: python django python-3.4

我的Django应用程序中有以下代码

def get_filename(root):
    def wrapper(instance, filename):
        return (root + filename)

    return wrapper

...

docfile = models.FileField(upload_to=get_filename("/some_dir"))

使用python3 manage.py makemigrations myapp运行此操作会导致以下错误

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.4/dist-packages/django/core/management/base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.4/dist-packages/django/core/management/base.py", line 399, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.4/dist-packages/django/core/management/commands/makemigrations.py", line 150, in handle
    self.write_migration_files(changes)
  File "/usr/local/lib/python3.4/dist-packages/django/core/management/commands/makemigrations.py", line 178, in write_migration_files
    migration_string = writer.as_string()
  File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/writer.py", line 167, in as_string
    operation_string, operation_imports = OperationWriter(operation).serialize()
  File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/writer.py", line 124, in serialize
    _write(arg_name, arg_value)
  File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/writer.py", line 76, in _write
    arg_string, arg_imports = MigrationWriter.serialize(item)
  File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/writer.py", line 357, in serialize
    item_string, item_imports = cls.serialize(item)
  File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/writer.py", line 433, in serialize
    return cls.serialize_deconstructed(path, args, kwargs)
  File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/writer.py", line 318, in serialize_deconstructed
    arg_string, arg_imports = cls.serialize(arg)
  File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/writer.py", line 507, in serialize
    % (value.__name__, module_name, get_docs_version()))
ValueError: Could not find function wrapper in myapp.models.
Please note that due to Python 2 limitations, you cannot serialize unbound method functions (e.g. a method declared and used in the same class body). Please move the function into the main module body to use migrations.
For more information, see https://docs.djangoproject.com/en/1.9/topics/migrations/#serializing-values

我正在运行Python 3.4.3,Django使用我的Python 2安装吗?

3 个答案:

答案 0 :(得分:3)

Django正在您的系统上使用Python的默认安装。这可能是2.7

如果您想使用特定的Python版本,可以使用该版本明确启动Django

~$ python3 django-admin runserver --settings=myapp.settings

但你真正应该做的是安装virtualenv创建一个虚拟环境来运行Django。

它允许您指定要使用的Python版本,使用不同版本的Django,以及不同版本的模块用于不同的项目。所有各种模块的数千个混合版本都没有让您的系统混乱。

答案 1 :(得分:0)

最好设置一个python-3.4 virtualenv并从虚拟环境运行Django项目。

查找安装指南here以及更多信息here

答案 2 :(得分:0)

修改用于python3的命令。例如,为了启动django服务器:      python3 manage.py runserver 0.0.0.0:8000 代替      python manage.py runserver 0.0.0.0:8000