pathlib路径解析软件包的已安装路径目录,而不是源代码目录

问题描述:

我已经使用setup.py打包了我的项目,并且项目文件夹结构如下所示.

I have packaged my project using setup.py and project folder structure looks like below.

  api-automation
  api
    packagename
       __init__.py
       user.py
       payloads
         a.json
         b.json    
  tests
    conftest.py
  setup.cfg
  setup.py
  README.rst

我在下面的文件夹中创建了名为"myenv_1"的虚拟环境, /用户/basavarajlamani/文档/环境/ 并且我已经在此虚拟环境中安装了上述存储库.

I have created virtual environment in below folder with name "myenv_1", /Users/basavarajlamani/Documents/environments/ and i have installed above repo in this virtual environment.

我在*和Internet上做了很多尝试,但没有找到答案.

I tried a lot on * and internet but did not found answer.

user.py文件的代码

code of user.py file

from pathlib import Path

current_dir = str(Path(__file__).resolve().parent)

def func():
    print("current_dir", current_dir)

conftest.py的代码

code of conftest.py

from packagename.user import func

func()

如果我直接运行user.py文件(python3 user.py),我将获得如下正确的目录路径,

If I run user.py file directly(python3 user.py), i will get the correct directory path as below,

current_dir /Users/basavarajlamani/Documents/repos/api-automation/api/packagename

但是,如果我运行conftest.py文件(python3 conftest.py),我将得到以下安装路径,该路径是我不想要的,并且我希望获得目录路径,如直接运行user.py文件时一样,>

But if I run conftest.py file(python3 conftest.py), I am getting installed path as below which i don't want and I want to get directory path like when i run user.py file directly,

current_dir
/Users/basavarajlamani/Documents/environments/myenv_1/lib/python3.7/site-packages/packagename

请帮助我如何解决这个问题.

Please help, how i can solve this problem.

我怀疑您在引导开发环境时没有使用正确的选项.

I suspect you didn't use the correct option when bootstrapping your development environment.

尝试:

  • 清理您的开发virtualenv或将其删除并创建一个新的虚拟环境.
  • cd the/root/of/your/source/tree
  • pip install -e .
  • cleanup your development virtualenv or delete it and create a new one.
  • cd the/root/of/your/source/tree
  • pip install -e .

重点是-e选项.阅读pip手册.

The important point is the -e option. Read the pip manual.