I wrote a simple bash script to run a game I am making in Python. However, when I do this, it gives me an import error when trying to import custom modules.
我编写了一个简单的bash脚本来运行我用Python编写的游戏。但是,当我这样做时,当尝试导入自定义模块时,它会给我一个导入错误。
Traceback (most recent call last):
File "/home/user/Projects/Test/game.py", line 7, in <module>
import boot, splash, menu, game, player, options
ImportError: No module named 'boot'
I checked the permissions on the folder containing the modules and they are accessible. If I just run the game.py in Terminal as:
我检查了包含模块的文件夹的权限,它们是可访问的。如果我运行这个游戏。py在终端:
python3.4 game.py
Then everything works perfectly fine. No import errors or anything. Just when I run it as the Bash file. For reference, here is the Bash file I am using:
然后一切都很顺利。没有导入错误或任何东西。当我将它作为Bash文件运行时。这里是我使用的Bash文件:
#!/bin/bash
STRING="Launching game..."
PYTHON="/usr/bin/python3.4"
GAME="/home/user/Projects/Test/game.py"
echo $STRING
$PYTHON "$GAME"
EDIT: It appears to be an absolute/relative path issue as to where the Bash file is and the Python file. Running the Bash file, since the Python file is using relative path, makes it so it does not know where the source of the custom modules are.
编辑:对于Bash文件的位置和Python文件,这似乎是一个绝对/相对路径问题。运行Bash文件,因为Python文件使用的是相对路径,所以它不知道自定义模块的源在哪里。
1 个解决方案
#1
2
If you are concerned about relative vs. absolute paths (which do sound like they could be the potential cause here), I would recommend doing something like my code below to ensure that you use relative paths in the bash script also.
如果您关心相对路径和绝对路径(听起来它们可能是这里的潜在原因),我建议您做如下代码所示的事情,以确保您在bash脚本中也使用相对路径。
Essentially, you switch the cwd to the directory containing the game file, but leverage pushd and popd to ensure that the switch doesn't affect anything else. The redirection to /dev/null is to suppress output that comes from pushd/popd which is usually just extra noise in a script situation.
本质上,您将cwd切换到包含游戏文件的目录,但是利用pushd和popd确保切换不会影响其他任何内容。重定向到/dev/null是为了抑制来自pushd/popd的输出,而在脚本中这通常只是额外的噪音。
#!/bin/bash
STRING="Launching game..."
PYTHON="/usr/bin/python3.4"
GAME_ROOT="/home/user/Projects/Test"
GAME="game.py"
pushd . > /dev/null 2>&1
cd $GAME_ROOT
echo $STRING
$PYTHON "$GAME"
popd > /dev/null 2>&1
#1
2
If you are concerned about relative vs. absolute paths (which do sound like they could be the potential cause here), I would recommend doing something like my code below to ensure that you use relative paths in the bash script also.
如果您关心相对路径和绝对路径(听起来它们可能是这里的潜在原因),我建议您做如下代码所示的事情,以确保您在bash脚本中也使用相对路径。
Essentially, you switch the cwd to the directory containing the game file, but leverage pushd and popd to ensure that the switch doesn't affect anything else. The redirection to /dev/null is to suppress output that comes from pushd/popd which is usually just extra noise in a script situation.
本质上,您将cwd切换到包含游戏文件的目录,但是利用pushd和popd确保切换不会影响其他任何内容。重定向到/dev/null是为了抑制来自pushd/popd的输出,而在脚本中这通常只是额外的噪音。
#!/bin/bash
STRING="Launching game..."
PYTHON="/usr/bin/python3.4"
GAME_ROOT="/home/user/Projects/Test"
GAME="game.py"
pushd . > /dev/null 2>&1
cd $GAME_ROOT
echo $STRING
$PYTHON "$GAME"
popd > /dev/null 2>&1