简体   繁体   English

如何将 shell 中的所有环境变量传递给它调用的 bash 脚本?

[英]How do I pass all the environment variables from my shell to the bash script it calls?

I'm running Python 3.7, Django 2, on Mac OS X. When I run the following commands from the command line, they work seamlessly...我在 Mac OS X 上运行 Python 3.7、Django 2。当我从命令行运行以下命令时,它们可以无缝运行...

localhost:web davea$ source ./venv/bin/activate
(venv) localhost:web davea$ python manage.py migrate maps
System check identified some issues:

WARNINGS:
?: (mysql.W002) MySQL Strict Mode is not set for database connection 'default'
    HINT: MySQL's Strict Mode fixes many data integrity problems in MySQL, such as data truncation upon insertion, by escalating warnings into errors. It is strongly recommended you activate it. See: https://docs.djangoproject.com/en/2.0/ref/databases/#mysql-sql-mode
Operations to perform:
  Apply all migrations: maps
Running migrations:
  No migrations to apply.

However, when I create a bash script containing the commands但是,当我创建包含命令的 bash 脚本时

!#/bin/bash
source ./venv/bin/activate
python manage.py migrate maps

The script dies剧本死了

localhost:web davea$ sh test.sh 
test.sh: line 1: !#/bin/bash: No such file or directory
Traceback (most recent call last):
  File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 15, in <module>
    import MySQLdb as Database
  File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/MySQLdb/__init__.py", line 18, in <module>
    from . import _mysql
ImportError: dlopen(/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/MySQLdb/_mysql.cpython-37m-darwin.so, 2): Library not loaded: libmysqlclient.18.dylib
  Referenced from: /Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/MySQLdb/_mysql.cpython-37m-darwin.so
  Reason: image not found

The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 347, in execute
    django.setup()
  File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/apps/registry.py", line 112, in populate
    app_config.import_models()
  File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/apps/config.py", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/contrib/auth/models.py", line 2, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/contrib/auth/base_user.py", line 47, in <module>
    class AbstractBaseUser(models.Model):
  File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/db/models/base.py", line 114, in __new__
    new_class.add_to_class('_meta', Options(meta, app_label))
  File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/db/models/base.py", line 315, in add_to_class
    value.contribute_to_class(cls, name)
  File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/db/models/options.py", line 205, in contribute_to_class
    self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
  File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/db/__init__.py", line 33, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/db/utils.py", line 202, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/db/utils.py", line 110, in load_backend
    return import_module('%s.base' % backend_name)
  File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 20, in <module>
    ) from err
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.
Did you install mysqlclient?

I have figured out with the help of my friends in comments, that the problem is due to some env vars not being accessible from my bash script.我在评论中的朋友的帮助下发现问题是由于我的 bash 脚本无法访问某些环境变量。 Rather than cherry-pick specific vars, I would like to know how to pass my entire set of environment vars to the script before it runs.我想知道如何在脚本运行之前将我的整个环境变量集传递给脚本,而不是挑选特定的变量。

Don't use sh to run shell scripts (unless that's really the shell you want, which is rare).不要使用sh运行 shell 脚本(除非这确实是您想要的 shell,这很少见)。 sh is not the same shell as bash . shshellbash Either invoke it with bash instead, or fix your !#/bin/bash shebang to #!/bin/bash and simply invoke the script directly;要么使用bash调用它,要么将你的!#/bin/bash shebang修复为#!/bin/bash并直接调用脚本; this is generally easier to manage (and fewer characters to type...).这通常更容易管理(输入的字符更少......)。

I suspect simply using the correct shell will be enough to solve your problem, but if not you need to figure out where DYLD_LIBRARY_PATH is being set (presumably either your ~/.bashrc or venv/bin/activate ) and ensure the variable is being export -ed.我怀疑只需使用正确的 shell 就足以解决您的问题,但如果不是,您需要弄清楚DYLD_LIBRARY_PATH的设置位置(大概是您的~/.bashrcvenv/bin/activate )并确保正在export变量-ed。 Bash variables are not environment variables by default, you need to export them in order to tell bash to include them in the env variables for any subprocesses that are invoked. Bash 变量默认不是环境变量,您需要export它们以告诉 bash 将它们包含在调用的任何子进程的 env 变量中。

You can change your start line as below您可以如下更改起点线

#!/bin/bash -l
source ./venv/bin/activate
python manage.py migrate maps

The -l flag will make sure that your bash profile is loaded -l标志将确保您的 bash 配置文件已加载

I would try script this:我会尝试编写这样的脚本:

!#/bin/bash
./venv/bin/python manage.py migrate maps

Explanation:解释:

from "Creation of virtual environments" https://docs.python.org/3/library/venv.html来自“创建虚拟环境” https://docs.python.org/3/library/venv.html

You don't specifically need to activate an environment;您不需要特别激活环境; activation just prepends the virtual environment's binary directory to your path, so that “python” invokes the virtual environment's Python interpreter and you can run installed scripts without having to use their full path.激活只是将虚拟环境的二进制目录添加到您的路径中,以便“python”调用虚拟环境的 Python 解释器,您可以运行已安装的脚本而无需使用它们的完整路径。 However, all scripts installed in a virtual environment should be runnable without activating it, and run with the virtual environment's Python automatically.但是,安装在虚拟环境中的所有脚本都应该可以在不激活的情况下运行,并自动与虚拟环境的 Python 一起运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将命令行参数传递给我的Python manage.py shell脚本? - How do I pass command line arguments to my Python manage.py shell script? 如何从bash脚本运行django shell命令 - How can I run django shell commands from a bash script 如何将HTML模板页面中的Javascript变量传递给Django视图函数? - How do I pass Javascript variables from my html template page to a Django views function? 如何使用 bash shell 找到我计算机的 IP 地址? - How do I find my computer's IP address using the bash shell? Dockerfile-如何将文件中的密码传递到ENV变量中? - Dockerfile - how do I pass passwords from a file into ENV variables? 有没有办法将环境变量传递给 python 脚本? - Is there a way to pass in environment variables to python script? 如何将变量从shell脚本传递给python脚本然后保存 - How to pass a variable from a shell script to a python script and save it then 如何在我的django应用程序中使用venv bash中创建的变量? - How do I use variables created in venv bash in my django app? Django 1.9:将变量从python脚本传递到bash脚本-OSError:[Errno 2]没有这样的文件或目录 - Django 1.9 : Pass variables from python script to bash script - OSError : [Errno 2] No such file or directory 如何将Shell环境变量传递给Supervisor程序? - How to pass shell environment variable to Supervisor program?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM