我的项目结构:
root
|-Pipfile
|-main.py
|
|-first_plugin
| |-Pipfile
| |-main.py
|-second_plugin
| |-Pipfile
| |-main.py
这些项目的main.py
功能是:
- 运行
git clone
以下载每个插件Github
- 运行
pipenv install
以安装它们的依赖项 run()
从每个插件的main.py
.
root/Pipfile
:_
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"
[packages]
GitPython = ">=3.1"
PyYAML = ">=6.0"
[dev-packages]
[requires]
python_version = "3.8"
first_plugin/Pipfile
:_
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"
[packages]
"-e ." = "*"
PyYAML = ">=6.0"
requests = "*"
[dev-packages]
[requires]
python_version = "3.8"
first_plugin/main.py
:_
from Plugins.plugin import Plugin
import requests
class FirstPlugin(Plugin):
def run(self):
if self.is_activated:
print("The First Plugin is being run..")
else:
print("The First Plugin can not run because it is not activated")
该函数read_from_yml
下载带有git clone
nad 运行的插件pipenv install
以安装它们的依赖项:
import yaml
import git
import os
import shutil
import subprocess as sp
import sys
"""
Reads plugins from a Yaml file and downloads them
with git clone if there is not a subdirectory with their
name in the Plugins directory
The format of the Yaml file must be as shown bellow:
- It must contain a single list of dictionaries that must have the keys bellow :
- name : The name of the plugin
- url : a url of the git repo in which the plugin is stored
"""
def read_from_yml():
#Open the yml file
with open("plugins.yml", "r") as stream:
try:
plugins = yaml.safe_load(stream)
#Get all the subdirectories of the Plugins directory
PluginFolder = "Plugins/"
possible_plugins = os.listdir(PluginFolder)
for plugin in plugins:
#if the plugin does not have a url, do nothing
if plugin["url"] == '':
print("Not in the web")
else:
#if the plugin has a url, try to remove the directory of the
# plugin from the Plugins directory. We do that in order to download the latest version of
# the plugin from the internet
try:
shutil.rmtree(PluginFolder + plugin["name"])
except OSError as e:
print("Error: %s - %s." % (e.filename, e.strerror))
# Git clone the repository of the plugin into the Plugins directory
git.Repo.clone_from(plugin["url"], os.path.join(os.getcwd(), PluginFolder + plugin["name"]))
all_files = os.listdir(PluginFolder + plugin["name"])
# Check if there is a requirements.txt file and install
# the dependencies it contains
if "Pipfile" in all_files:
os.chdir(os.getcwd() + "/" + PluginFolder + plugin["name"])
print(os.getcwd())
sp.check_call(["pipenv", "install"])
sp.check_call(["pipenv", "graph"])
new_cwd = os.getcwd().replace("/" + PluginFolder + plugin["name"], '')
os.chdir(new_cwd)
print(os.getcwd())
except yaml.YAMLError as exc:
print(exc)
问题 :
当我使用 cmd 运行项目时,python3 main.py
插件的依赖项会按预期安装。但是当我使用 cmd 时pipenv run python3 main.py
,没有安装插件的依赖项,并且程序在第一个插件处崩溃并显示以下错误消息:
File "Plugins/First_plugin/main.py", line 2, in <module>
import requests
ModuleNotFoundError: No module named 'requests'
编辑 ;
此 cmdsp.check_call(["pipenv", "graph"])
打印所有由pipenv
. 我在First_plugin
目录中调用它,这就是结果
PyYAML==6.0
requests==2.26.0
- certifi [required: >=2017.4.17, installed: 2021.10.8]
- charset-normalizer [required: ~=2.0.0, installed: 2.0.9]
- idna [required: >=2.5,<4, installed: 3.3]
- urllib3 [required: >=1.21.1,<1.27, installed: 1.26.7]
这意味着requests
软件包已安装。但是程序之后仍然会打印上一条错误消息:
File "Plugins/First_plugin/main.py", line 2, in <module>
import requests
ModuleNotFoundError: No module named 'requests'