人们-这个问题困扰了我一段时间,并且仅在我使用的特定系统上。我的解决方法是在python虚拟环境之外安装python软件包,但我只想解决此问题。我已经寻找了数周的解决方案,但没有任何运气。我在Windows 10上使用Visual Studio Code。
需要帮助
我想解决以下问题,并避免上述解决方法,但一直无法纠正。对于造成该问题的根本原因的任何帮助将不胜感激。
问题
当我执行虚拟环境设置和pip安装时:
PS D:\Code\my-pkg> python -m venv venv
PS D:\Code\my-pkg> .\venv\Scripts\activate
(venv) PS D:\Code\my-pkg>pip install -e ".[test]" --force
我应该能够从命令行运行“ publish.py”:
(venv) PS D:\Code\my-pkg> publish
但是,当我尝试运行它时,得到以下信息:
Traceback (most recent call last):
File "D:\Code\my-pkg\venv\Scripts\publish.py", line 4, in <module>
__import__('pkg_resources').require('my-pkg==0.2.0.0')
File "C:\Python37\lib\site-packages\pkg_resources\__init__.py", line 3088, in <module>
@_call_aside
File "C:\Python37\lib\site-packages\pkg_resources\__init__.py", line 3072, in _call_aside
f(*args, **kwargs)
File "C:\Python37\lib\site-packages\pkg_resources\__init__.py", line 3101, in _initialize_master_working_set
working_set = WorkingSet._build_master()
File "C:\Python37\lib\site-packages\pkg_resources\__init__.py", line 574, in _build_master
ws.require(__requires__)
File "C:\Python37\lib\site-packages\pkg_resources\__init__.py", line 892, in require
needed = self.resolve(parse_requirements(requirements))
File "C:\Python37\lib\site-packages\pkg_resources\__init__.py", line 778, in resolve
raise DistributionNotFound(req, requirers)
pkg_resources.DistributionNotFound: The 'my-pkg==0.2.0.0' distribution was not found and is required by the application
解决方法
要解决此问题,我必须直接调用脚本:
(venv) PS D:\Code\my-pkg> .\src\Scripts\publish.py
产生的结果
Traceback (most recent call last):
File "D:\Code\my-pkg\src\Scripts\publish.py", line 5, in <module>
import click
ModuleNotFoundError: No module named 'click'
我可以通过在虚拟环境之外的系统上安装click来解决此问题。但是,我需要对我可能拥有的任何其他软件包依赖项执行此操作,这违背了使用virtualenv的目的。
setup.py
from setuptools import setup, find_packages
import os
build_num = os.environ.get('BUILD_NUMBER', '0')
version = "0.2.0.{}".format(build_num)
test_requirements = ['pytest', 'pylint', 'coverage', 'pytest-cov']
setup(
name="my-pkg",
version=version,
packages=find_packages(),
scripts=[
'src/Scripts/publish.py'
],
tests_require=test_requirements,
install_requires=[
'requests',
'requests_negotiate_sspi',
'wincertstore',
'click'
],
extras_require={
'test':test_requirements
}
)
publish.py
import logging
import click
@click.group()
def tests():
return 0
@tests.command()
@click.argument("endpoint")
@click.argument("results_path")
@click.option("--format", "-i", type=click.Choice("nunit", "junit"), required=True)
@click.option("--logging_level", "-l", type=click.Choice(logging.INFO, logging.DEBUG), default=logging.INFO, show_default=True)
def find(endpoint, results_path, format, logging_level):
logging.basicConfig(level=logging_level)
logging.info("Hello, friend")