我对Python和Django都比较陌生,而且我有一个由别人开发的应用程序。我正在尝试编写一个自动打开SSH隧道的脚本,并从我的本地机器运行命令到我的远程postgres数据库,以便我可以在本地进行解析 - 这通常是通过键入{{1 }}。这在我run a manual SSH tunnel之前有效,但现在想要将其设置为在脚本中运行并自动关闭连接。我的脚本目前设置如下:
python manage.py parse
当我运行脚本(位于我的应用的根文件中)时,我现在收到以下错误:
import psycopg2
import sshtunnel
from django.core.management import execute_from_command_line
sshtunnel.SSH_TIMEOUT = 5.0
sshtunnel.TUNNEL_TIMEOUT = 5.0
with sshtunnel.SSHTunnelForwarder(
('ssh.pythonanywhere.com'),
ssh_username='ssh_username', ssh_password='ssh_password',
remote_bind_address=('username2.postgres.pythonanywhere-services.com', port)
) as tunnel:
params = {
"dbname": 'dbname',
"user": 'user',
"password": 'password',
"host": 'host',
"port": tunnel.local_bind_port,
}
connection = psycopg2.connect(**params)
cursor = connection.cursor()
execute_from_command_line(["manage.py", "parse"])
cursor.close
connection.close()
在我看来,这肯定是一个我没有改变某个地方脚本名称的问题,但是通过我的系统进行了搜索,我似乎无法找到' yoursite& #39;在我的任何代码中。有没有人知道这个错误所指的代码在哪里?
Settings.py内容:
/home/user/anaconda3/lib/python3.6/site-packages/psycopg2/__init__.py:144: UserWarning: The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use "pip install psycopg2-binary" instead. For details see: <http://initd.org/psycopg/docs/install.html#binary-install-from-pypi>.
""")
Traceback (most recent call last):
File "ssh_tunnel.py", line 24, in <module>
execute_from_command_line(["manage.py", "parse"])
File "/home/user/anaconda3/lib/python3.6/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
utility.execute()
File "/home/user/anaconda3/lib/python3.6/site-packages/django/core/management/__init__.py", line 307, in execute
settings.INSTALLED_APPS
File "/home/user/anaconda3/lib/python3.6/site-packages/django/conf/__init__.py", line 56, in __getattr__
self._setup(name)
File "/home/user/anaconda3/lib/python3.6/site-packages/django/conf/__init__.py", line 41, in _setup
self._wrapped = Settings(settings_module)
File "/home/user/anaconda3/lib/python3.6/site-packages/django/conf/__init__.py", line 110, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/home/user/anaconda3/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'yoursite'
授权详情已更改。
结构是:
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SECRET_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
DEBUG = True
ALLOWED_HOSTS = ['*']
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'api',
'taggit',
'rest_framework',
'taggit_serializer',
'django.contrib.sites',
'rest_framework.authtoken',
'rest_auth',
'allauth',
'allauth.account',
'rest_auth.registration',
'allauth.socialaccount',
'allauth.socialaccount.providers.facebook',
'allauth.socialaccount.providers.twitter',
'corsheaders',
)
MIDDLEWARE_CLASSES = (
'corsheaders.middleware.CorsMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
)
ROOT_URLCONF = 'mysite.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'api.context_processors.menubar'
# 'social_django.context_processors.backends',
# 'social_django.context_processors.login_redirect',
],
},
},
]
WSGI_APPLICATION = 'mysite.wsgi.application'
CORS_ORIGIN_ALLOW_ALL = True
DATABASES = {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'username',
'USER': 'user',
'PASSWORD': 'password',
}
}
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'HOST': 'xxxxxxxx',
'NAME': 'name',
'USER': 'user',
'PASSWORD': 'password',
}
}
else:
DATABASES['default']['HOST'] = '127.0.0.1'
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
STATIC_ROOT = 'static/'
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
'rest_framework.authentication.SessionAuthentication',
"allauth.account.auth_backends.AuthenticationBackend",
)
REST_FRAMEWORK = {
'PAGE_SIZE': 10,
'DEFAULT_AUTHENTICATION_CLASSES': (
'api.permissions.POSTOnlyAuthentication',
),
}
SESSION_SERIALIZER = "django.contrib.sessions.serializers.PickleSerializer"
SITE_ID = 1
AUTH_USER_MODEL = 'api.MyUser'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'app@gmail.com'
EMAIL_HOST_PASSWORD = 'app.development'
EMAIL_PORT = 587
ACCOUNT_EMAIL_VERIFICATION = 'optional'
ACCOUNT_EMAIL_REQUIRED = True
LOGIN_URL = LOGIN_REDIRECT_URL = 'api/auth/login/'
ACCOUNT_CONFIRM_EMAIL_ON_GET = True
ACCOUNT_USERNAME_REQUIRED = True
ACCOUNT_AUTHENTICATION_METHOD = 'username'
ACCOUNT_EMAIL_CONFIRMATION_ANONYMOUS_REDIRECT_URL = LOGIN_URL