简体   繁体   English

如何使用 Anaconda 在后台运行 python 脚本? ('nohup python -u test.py &' 不起作用!)

[英]How to run python script in background using Anaconda? ('nohup python -u test.py &' doesn't work!)

I have a simple python script test.py :我有一个简单的 python 脚本test.py

import time
import logging

logging.basicConfig(filename='app.log', filemode='w', level=logging.DEBUG)
i=0

while i<100:
    i+=1
    logging.info(i)
    print(i)
    time.sleep(1)

I want to run this script in background using anaconda. I tried: nohup python -u test.py & .我想使用 anaconda 在后台运行这个脚本。我试过: nohup python -u test.py & python keyword invokes anaconda on my machine. python关键字在我的机器上调用 anaconda。 It seems that script is still linked to the terminal I used to run it.似乎脚本仍然链接到我用来运行它的终端。 If I close the terminal, the execution stops and if I use 'exit' to close the terminal, the terminal turns black but doesn't close.如果我关闭终端,执行将停止,如果我使用“退出”关闭终端,终端会变黑但不会关闭。 If I close using 'X', the execution stops.如果我使用“X”关闭,则执行停止。

What is the correct way to trigger a python script to run on anaconda in background?触发 python 脚本在后台运行 anaconda 的正确方法是什么?

$ conda info

     active environment : None
          conda version : 4.9.2
    conda-build version : 3.20.5
         python version : 3.8.5.final.0
       virtual packages : __win=0=0
                          __archspec=1=x86_64
       base environment : F:\Automation\Anaconda3  (read only)
           channel URLs : https://repo.anaconda.com/pkgs/main/win-64
                          https://repo.anaconda.com/pkgs/main/noarch
                          https://repo.anaconda.com/pkgs/r/win-64
                          https://repo.anaconda.com/pkgs/r/noarch
                          https://repo.anaconda.com/pkgs/msys2/win-64
                          https://repo.anaconda.com/pkgs/msys2/noarch
               platform : win-64
             user-agent : conda/4.9.2 requests/2.24.0 CPython/3.8.5 Windows/10 Windows/10.0.17763
          administrator : False
             netrc file : None
           offline mode : False

Terminal used to run script: Git , version: 2.29.2.windows.3用于运行脚本的终端: Git ,版本: 2.29.2.windows.3

Use the python executable of your conda environment.使用您的 conda 环境的 python 可执行文件。

  1. run conda info to get the path to the base environment:<base_environment_path>运行 conda info 获取基础环境的路径:<base_environment_path>
  2. to use specific env, use its name and then access the python bin executable:要使用特定的环境,请使用其名称,然后访问 python bin 可执行文件:
  3. nohup <base_environment_path>/envs/<env-name>/bin/python <script_name>.py

eg例如

nohup /home/ubuntu/anaconda3/envs/my-env/bin/python test.py > output.txt &

You will need to know the path to the python executable in the environment you want to execute the code in.您将需要知道要在其中执行代码的环境中的 python 可执行文件的路径。

You can find that by running你可以通过运行找到

conda info康达信息

in your desired environment.在您想要的环境中。 When you find the location of environment you just have to to do this:当您找到环境的位置时,您只需执行以下操作:

nohup <absolute path to your anaconda environment>/bin/python <YOUR SCRIPT> > output.txt &

If you don't want to use conda info to find it you can just execute this如果你不想使用 conda info 来找到它,你可以执行这个

nohup <absolute path to your anaconda>/anaconda3/envs/<your environment>/bin/python <YOUR SCRIPT> > output.txt &

I have a simple python script test.py :我有一个简单的 python 脚本test.py

import time
import logging

logging.basicConfig(filename='app.log', filemode='w', level=logging.DEBUG)
i=0

while i<100:
    i+=1
    logging.info(i)
    print(i)
    time.sleep(1)

I want to run this script in background using anaconda.我想使用 anaconda 在后台运行此脚本。 I tried: nohup python -u test.py & .我试过: nohup python -u test.py & python keyword invokes anaconda on my machine. python关键字在我的机器上调用 anaconda。 It seems that script is still linked to the terminal I used to run it.似乎该脚本仍然链接到我用来运行它的终端。 If I close the terminal, the execution stops and if I use 'exit' to close the terminal, the terminal turns black but doesn't close.如果我关闭终端,执行将停止,如果我使用“退出”关闭终端,终端会变黑但不会关闭。 If I close using 'X', the execution stops.如果我使用“X”关闭,则执行停止。

What is the correct way to trigger a python script to run on anaconda in background?触发 python 脚本在后台在 anaconda 上运行的正确方法是什么?

$ conda info

     active environment : None
          conda version : 4.9.2
    conda-build version : 3.20.5
         python version : 3.8.5.final.0
       virtual packages : __win=0=0
                          __archspec=1=x86_64
       base environment : F:\Automation\Anaconda3  (read only)
           channel URLs : https://repo.anaconda.com/pkgs/main/win-64
                          https://repo.anaconda.com/pkgs/main/noarch
                          https://repo.anaconda.com/pkgs/r/win-64
                          https://repo.anaconda.com/pkgs/r/noarch
                          https://repo.anaconda.com/pkgs/msys2/win-64
                          https://repo.anaconda.com/pkgs/msys2/noarch
               platform : win-64
             user-agent : conda/4.9.2 requests/2.24.0 CPython/3.8.5 Windows/10 Windows/10.0.17763
          administrator : False
             netrc file : None
           offline mode : False

Terminal used to run script: Git , version: 2.29.2.windows.3用于运行脚本的终端: Git ,版本: 2.29.2.windows.3

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

相关问题 如何比较分别存储在脚本test.py(Python)中2个不同函数中的2个字典 - How to compare 2 dictionaries stored sperately in 2 different functions within the script test.py(Python) python manage.py migration在anaconda上不起作用 - python manage.py migrate doesn't work on anaconda Nohup和Python -u:它仍然不会实时记录数据 - Nohup and Python -u : it still doesn't log data in realtime 使用 nohup 时,python 脚本无法工作 - python script fails to work when using nohup 当您只是从其他地方(例如 main.py)导入一段 test.py 时,为什么要运行 python 文件(例如 test.py)的其他代码行? - Why do you run other lines of codes of a python file(say test.py) when you are just importing a piece of test.py from somewhere else (say main.py)? &#39;/ usr / bin / python3:在Raspberry Pi上使用mono执行.exe文件时无法打开文件&#39;test.py&#39;&#39; - '/usr/bin/python3: Can't Open File 'test.py'' when executing .exe file using mono on Raspberry Pi 如何从 Python 脚本运行 nohup 命令? - How to run nohup command from Python script? 在nohup中使用python脚本 - using python script with nohup Python 脚本在 anaconda3 上运行时无法在 cmd 上运行 - Python script doesn't run on cmd when it runs on anaconda3 &#39;%%file test.py&#39; 在 python 中是什么意思? - what does '%%file test.py' mean in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM