解决运行磨工脚本时的导入错误:没有名为的模块

2 投票
3 回答
1677 浏览
提问于 2025-04-18 05:23

我开始使用grinder工具,每次运行我的脚本时都会出现以下错误。

ImportError: No module named net.grinder.script. Grinder

这是我尝试运行的脚本。

# A minimal script that tests The Grinder logging facility.
#
# This script shows the recommended style for scripts, with a
# TestRunner class. The script is executed just once by each worker
# process and defines the TestRunner class. The Grinder creates an
# instance of TestRunner for each worker thread, and repeatedly calls
# the instance for each run of that thread.

  from net.grinder.script.Grinder import grinder
  from net.grinder.script import Test

# A shorter alias for the grinder.logger.info() method.
  log = grinder.logger.info

# Create a Test with a test number and a description. The test will be
# automatically registered with The Grinder console if you are using
# it.
  test1 = Test(1, "Log method")

# Instrument the info() method with our Test.
  test1.record(log)

# A TestRunner instance is created for each thread. It can be used to
# store thread-specific data.
 class TestRunner:

# This method is called for every run.
 def __call__(self):
    log("Hello World")

每次运行这个脚本时,我都会遇到导入错误。

我已经设置了CLASSPATH和JAVA_HOME这两个环境变量。有没有人能帮帮我呢?

3 个回答

0

你能帮我确认一下我发的细节吗 @

http://stackoverflow.com/questions/19148365/cant-run-grinder-java-test-framework/19429771#19429771

看起来是路径的问题。如果上面提到的解决办法都不行,试着手动设置一下路径,用 'sys.path()' 这个命令,然后在一个示例的 Jython 脚本里打印出来,把结果贴到这里。

print sys.path
1

看起来你是想直接用Jython来运行这个脚本。其实你应该用The Grinder来运行这个脚本。

如果你已经正确设置了类路径,并且脚本文件在本地目录里,名字叫做helloworld.py,那么

java -Dgrinder.script=helloworld.py net.grinder.Grinder

就会启动一个代理,运行一个工作进程,并且执行你的脚本一次。

1

设置类路径有点复杂,我在运行上面Philp提供的java命令之前,在Mac(10.6.8)的终端上做了以下操作:

类路径设置:(在终端上一个接一个地执行这些命令)

    export JAVA_VERSION=1.6
    export JAVA_HOME=/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
    export GRINDERPATH=/Users/cdname/grinder-3.11
    export CLASSPATH=$GRINDERPATH/lib/grinder.jar:$CLASSPATH
    export PATH=$JAVA_HOME/bin:$PATH
    java -classpath $CLASSPATH net.grinder.Grinder

我把这个添加到 /etc/launchd.conf 文件中,然后运行 grep -E "^setenv" /etc/launchd.conf | xargs -t -L 1 launchctl

在 grinder.properties 文件中设置: grinder.useConsole = false

现在运行 .py 文件:

     java -Dgrinder.script=csaTest.py net.grinder.Grinder

撰写回答