Python 进口的问题很奇怪

Python 进口的问题很奇怪,python,django,virtualenv,stashy,Python,Django,Virtualenv,Stashy,我遇到了一个非常奇怪的问题 编辑:添加完整详细信息: virtualenv -p python3 testenv cd testenv source bin/activate django-admin startproject blah cd blah mkdir modules vi modules/stash.py pip install django pip install stashy python manage.py shell < modules/stash.py 我使用以下

我遇到了一个非常奇怪的问题

编辑:添加完整详细信息:

virtualenv -p python3 testenv
cd testenv
source bin/activate
django-admin startproject blah
cd blah
mkdir modules
vi modules/stash.py
pip install django
pip install stashy
python manage.py shell < modules/stash.py
我使用以下命令运行脚本:

$ python  manage.py shell < modules/stash.py
从我很久以前做的Python来看,这似乎非常出乎意料,迫使我在每个方法中添加import。。。不确定导入依赖项的方式或运行脚本的方式是否有问题

编辑:更多详细信息,错误消息:

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/[PATH]/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "/[PATH]/python3.6/site-packages/django/core/management/__init__.py", line 365, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/[PATH]/python3.6/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/[PATH]/python3.6/site-packages/django/core/management/base.py", line 335, in execute
    output = self.handle(*args, **options)
  File "/[PATH]/python3.6/site-packages/django/core/management/commands/shell.py", line 92, in handle
    exec(sys.stdin.read())
  File "<string>", line 6, in <module>
  File "<string>", line 4, in __init__
NameError: name 'stashy' is not defined
回溯(最近一次呼叫最后一次):
文件“manage.py”,第15行,在
从命令行(sys.argv)执行命令
文件“/[PATH]/python3.6/site packages/django/core/management/_init__.py”,第371行,从命令行执行
utility.execute()
文件“/[PATH]/python3.6/site packages/django/core/management/_init__.py”,第365行,在execute中
self.fetch_命令(子命令)。从_argv(self.argv)运行_
文件“/[PATH]/python3.6/site packages/django/core/management/base.py”,第288行,在运行时从
self.execute(*args,**cmd_选项)
文件“/[PATH]/python3.6/site packages/django/core/management/base.py”,执行中的第335行
输出=self.handle(*args,**选项)
文件“/[PATH]/python3.6/site packages/django/core/management/commands/shell.py”,第92行,在handle中
exec(sys.stdin.read())
文件“”,第6行,在
文件“”,第4行,在_init中__
NameError:未定义名称“stash”

包中的
connect
函数只是一个包装器(如下所示)

您可以尝试通过提取隐藏对象本身来绕过错误:

from stashy.client import Stash

class SyncProject:
    def __init__(self, endpo, user, passw):
        self.stash = Stash(endpo, user, passw, verify=True)

我已经在我的末端复制了同样的问题。我按照问题中提到的步骤进行了尝试。我也有同样的问题


之后,我安装了django==1.10。它是有效的。检查它是否适合您。

我在Django项目中跟踪到的行为:
exec(sys.stdin.read())


这将导致您的第一个
import stashy
被限定为
命令
类。如果行是
exec(sys.stdin.read(),globals())
,则不会出现这种意外行为。对Django的这一更改还将消除重定向到stdin的代码与手动键入Django shell/Python REPL的代码之间的当前错误/行为差异。

首先,修复缩进。第二,张贴完整的脚本;这不会导致问题,因为没有任何东西正在实例化您的SyncProject类。已通过调用SyncProject(…)更新您仍然需要修复缩进。您使用的是哪个版本的Django?请发布异常的完整堆栈跟踪,以便我们可以看到发生异常时正在运行哪些方法。问题是,我拥有的每个依赖项都会发生这种情况。因此,这并不能解决真正的问题。那么请将此添加到问题中。如果我们无法重现您的错误,我们将无法帮助您。对不起,我真的不想用过时的依赖项启动一个新项目谢谢您这个网络陌生人!正在尝试合并修复程序:
Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/[PATH]/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "/[PATH]/python3.6/site-packages/django/core/management/__init__.py", line 365, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/[PATH]/python3.6/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/[PATH]/python3.6/site-packages/django/core/management/base.py", line 335, in execute
    output = self.handle(*args, **options)
  File "/[PATH]/python3.6/site-packages/django/core/management/commands/shell.py", line 92, in handle
    exec(sys.stdin.read())
  File "<string>", line 6, in <module>
  File "<string>", line 4, in __init__
NameError: name 'stashy' is not defined
__version__ = "0.1"

from .client import Stash

def connect(url, username, password, verify=True):
    """Connect to a Stash instance given a username and password.
    This is only recommended via SSL. If you need are using
    self-signed certificates, you can use verify=False to ignore SSL
    verifcation.
    """
    return Stash(url, username, password, verify=verify)

__all__ = ['connect']
from stashy.client import Stash

class SyncProject:
    def __init__(self, endpo, user, passw):
        self.stash = Stash(endpo, user, passw, verify=True)