Атрымаць шлях выканання кансольнага скрыпта Python (Get the runpath of a python console-script)


問題描述

Атрымаць шлях выканання кансольнага скрыпта Python (Get the runpath of a python console‑script)

Short of it ‑ I need to get a console_script to return to me its current run path, so that I can override modules it is using at run time.

Here is my basic setup (summarized as best I could):

    package_1:
      caller_module.py

    console_script_module:
      main.py
      dir_of_modules_to_use/
        a.py
        b.py
        c.py
      setup.py

    setup.py contents:
      setup(
        entry_points = {
          'console_scripts': [
            'console‑script‑module = console_script_module.main:main'
           ]
         }
        )         

Longer detail: So caller_module.py calls console‑script‑module by issuing a subprocess call.  In turn the modules seen in dir_of_modules_to_use are run.  I would like to provide my own version of those modules by overriding them right before this happens through a separate script.  In order to do this I need to know the run path of where console‑script‑module has been installed as it is not consistent (changes in virtualenv's for example).  

I tried adding this in to main.py and using a separate command line argument to call it:

    def print_absoulute_dir():    
      print os.path.abspath('dir_of_modules_to_use')

Unfortunately this only returns the path of wherever I make the call to the console script.

DISCLAIMER ‑ this is hacky and awful I know, it's from code I inherited and I just need something working in the short term.  I unfortunately cannot change the code within caller_module.py at this time, otherwise I would just change how it is calling console_script_module:main.py.

‑‑‑‑‑

參考解法

方法 1:

I'm not sure if i really understand what you want, but  every python module has the __file__ attribute, which contains its location in the filesystem,  and you can use sys.path to modify python module search path

import urllib
import sys

print urllib.__file__
print sys.path

(by rikityplikityChristian Thieme)

參考文件

  1. Get the runpath of a python console‑script (CC BY‑SA 3.0/4.0)

#runtime #Python #setup.py






相關問題

通過java運行的執行程序似乎超時? (Executed programs run through java seem to time out?)

Kesalahan NullPointerException dalam kode Java saya. (NullPointerException errors in my Java code.)

Hentikan proses anak ketika proses induk berhenti (Stop child process when parent process stops)

Атрымаць шлях выканання кансольнага скрыпта Python (Get the runpath of a python console-script)

選擇不包含主要類型 - 錯誤 (Selection does not contain main type - error)

在活動之間使用意圖錯誤 (using intents between activities error)

在運行時使用 Spring 在表中創建新字段 (Create new field into a table with Spring at runtime)

如何添加運行時超時以防止 java.net.SocketTimeoutException? (How do I add a runtime timeout to prevent java.net.SocketTimeoutException?)

我有什麼方法可以覆蓋 Java 中的系統屬性嗎? (Do I have any method to override System Properties in Java?)

如何計算此代碼的時間複雜度? (How can I compute the time complexity of this code?)

運行 .exe 文件生成 bt jar2exe 軟件時出現 java runtime environment not found 錯誤 (Getting java runtime enviroment not found error while running .exe file produced bt jar2exe software)

在運行時在 VBA 中顯示所有變量及其值 (Show all variables and their values in VBA during runtime)







留言討論