Это также может произойти, если есть проблема с сетью. Это означает, что даже несмотря на то, что «сценарии jquery» находятся на месте и включены до использования, поскольку jquery-скрипты недоступны, во время загрузки страницы, следовательно, определения в «$» рассматриваются как «неопределенные ссылки».
ДЛЯ НАЗНАЧЕНИЙ TEST / DEBUG :: Вы можете попытаться получить доступ к URL-адресу «jquery-script» в браузере. Если он доступен, ваша страница должна загружаться должным образом, иначе она покажет указанную ошибку (или другие ошибки, связанные с сценарием). Пример - http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js должен быть доступен в браузере (или в контексте браузера).
У меня была аналогичная проблема, в которой я смог загрузить html-страницу (используя скрипты) в моем браузере Windows-хоста, но не смог загрузить в vm-ubuntu. Решение проблемы с сетью позволило решить проблему.
Вы можете использовать parser.error
:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--action', choices=['upload', 'dump'], default='dump')
parser.add_argument('--dump-format')
args = parser.parse_args()
if args.action != 'dump' and args.dump_format:
parser.error('--dump-format can only be set when --action=dump.')
Другой способ подойти к проблеме - использовать подкоманды (a'la git) с «действием» в качестве первого аргумента:
script dump --dump-format="foo"
script upload
Модуль argparse предлагает способ сделать это, не выполняя собственные проверки на соответствие требованиям. В приведенном ниже примере используются «подпарамеры» или «подкоманды». Я реализовал подпараметр для «дампа» и один для «формата».
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('file', help='The file you want to act on.')
subparsers = parser.add_subparsers(dest='subcommand')
# subparser for dump
parser_dump = subparsers.add_parser('dump')
# add a required argument
parser_dump.add_argument(
'format',
choices=['csv', 'json'],
help='Dump the file in this format.')
# subparser for upload
parser_upload = subparsers.add_parser('upload')
# add a required argument
parser_upload.add_argument(
'server',
choices=['amazon', 'imgur'],
help='Upload the file to this service.')
args = parser.parse_args()
print args
if args.subcommand == 'dump':
print 'I will now dump "%s" in the %s format' % (args.file, args.format)
if args.subcommand == 'upload':
print 'I will now upload "%s" to %s' % (args.file, args.server)
В командной строке это выглядит так:
$ python ap.py
usage: ap.py [-h] file {upload,dump} ...
ap.py: error: too few arguments
$ python ap.py tmp.txt
usage: ap.py [-h] file {upload,dump} ...
ap.py: error: too few arguments
$ python ap.py tmp.txt upload
usage: ap.py file upload [-h] {amazon,imgur}
ap.py file upload: error: too few arguments
$ python ap.py tmp.txt upload amazo
usage: ap.py file upload [-h] {amazon,imgur}
ap.py file upload: error: argument server: invalid choice: 'amazo' (choose from 'amazon', 'imgur')
$ python ap.py tmp.txt upload amazon
Namespace(file='tmp.txt', server='amazon', subcommand='upload')
I will now upload "tmp.txt" to amazon
$ python ap.py tmp.txt upload imgur
Namespace(file='tmp.txt', server='imgur', subcommand='upload')
I will now upload "tmp.txt" to imgur
$ python ap.py tmp.txt dump
usage: ap.py file dump [-h] {csv,json}
ap.py file dump: error: too few arguments
$ python ap.py tmp.txt dump csv
Namespace(file='tmp.txt', format='csv', subcommand='dump')
I will now dump "tmp.txt" in the csv format
$ python ap.py tmp.txt dump json
Namespace(file='tmp.txt', format='json', subcommand='dump')
I will now dump "tmp.txt" in the json format
Дополнительная информация: http://docs.python.org/dev/library/argparse.html#argparse.ArgumentParser.add_subparsers