我有一個python應用程序,可以作爲系統服務運行。當我手動運行它時,應用程序運行正常。當我將它作爲服務運行時,它無法找到與pip install -e my_module
一起安裝的本地模塊。作爲systemd/systemctl服務運行時,本地模塊導入失敗python
主要應用有以下代碼:
print(sys.argv)
import pip
installed_packages = pip.get_installed_distributions()
installed_packages_list = sorted(["%s==%s" % (i.key, i.version) for i in installed_packages])
print(installed_packages_list)
print('doing tox')
import tox
print('doing my_mod')
import my_mod
print(my_mod.__file__)
from my_mod.auth.http_auth_provider import HTTPAuthProvider
當我手動運行它,我獲得(請注意,我-MOD被包括在「已安裝的軟件包」第二行):
['/usr/bin/pv_api']
['aiohttp==0.19.0', 'chardet==2.3.0', 'jsonschema==2.5.1', 'pip==7.0.0', 'pluggy==0.3.1', 'pv-api==0.0.0', 'py==1.4.31', 'pycrypto==2.6.1', 'pymongo==3.1.1', 'pyyaml==3.11', 'setuptools==19.6.2', 'six==1.10.0', 'tox==2.3.1', 'virtualenv==14.0.6', 'my-mod==0.1.0', 'webauthsession==1.1.1']
doing tox
doing my_mod
/root/my_module/my_mod/__init__.py
當通過服務日誌看起來是這樣運行(請注意,我-MOD,但不包括在「已安裝的軟件包」第二行)::
2016-02-26_00:39:01.90403 ['/usr/bin/pv_api']
2016-02-26_00:39:01.90406 ['aiohttp==0.19.0', 'chardet==2.3.0', 'jsonschema==2.5.1', 'pip==7.0.0', 'pluggy==0.3.1', 'pv-api==0.0.0', 'py==1.4.31', 'pycrypto==2.6.1', 'pymongo==3.1.1', 'pyyaml==3.11', 'setuptools==19.6.2', 'six==1.10.0', 'tox==2.3.1', 'virtualenv==14.0.6', 'webauthsession==1.1.1']
2016-02-26_00:39:01.90407 doing tox
2016-02-26_00:39:01.90407 doing my_mod
2016-02-26_00:39:01.90642 Traceback (most recent call last):
2016-02-26_00:39:01.90642 File "/usr/bin/pv_api", line 9, in <module>
2016-02-26_00:39:01.90642 load_entry_point('pv-api==0.0.0', 'console_scripts', 'pv_api')()
2016-02-26_00:39:01.90643 File "/usr/lib/python3.4/site-packages/pkg_resources/__init__.py", line 547, in load_entry_point
2016-02-26_00:39:01.90643 return get_distribution(dist).load_entry_point(group, name)
2016-02-26_00:39:01.90643 File "/usr/lib/python3.4/site-packages/pkg_resources/__init__.py", line 2719, in load_entry_point
2016-02-26_00:39:01.90643 return ep.load()
2016-02-26_00:39:01.90643 File "/usr/lib/python3.4/site-packages/pkg_resources/__init__.py", line 2379, in load
2016-02-26_00:39:01.90643 return self.resolve()
2016-02-26_00:39:01.90643 File "/usr/lib/python3.4/site-packages/pkg_resources/__init__.py", line 2385, in resolve
2016-02-26_00:39:01.90644 module = __import__(self.module_name, fromlist=['__name__'], level=0)
2016-02-26_00:39:01.90644 File "/usr/lib/python3.4/site-packages/pv/api/main.py", line 33, in <module>
2016-02-26_00:39:01.90644 import my_mod
2016-02-26_00:39:01.90644 ImportError: No module named 'my_mod'
這也可能是有用的信息:
[[email protected] etc]# ls -la /usr/lib/python3.4/site-packages/my-mod.egg-link
-rw-r--r-- 1 root root 37 Feb 26 00:20 /usr/lib/python3.4/site-packages/my-mod.egg-link
[[email protected] etc]# cat /usr/lib/python3.4/site-packages/my-mod.egg-link
/root/my_module
編輯:
你可以從「installed_packages」,它們通過requirements.txt安裝的所有其他包的輸出看到正確發現。當我作爲服務運行時,只有這個我有本地源代碼的庫不會被找到。 (當我在命令行中運行,或者當我從python3解釋器中運行import my_mod
研究發現。
這非常接近強調真正的問題。問題在於該服務正在以用戶身份運行,該用戶對安裝我的模塊的文件夾沒有讀取權限。 – user1753106