pathlib Path resolves installed path directory of package instead of source code directory

╄→гoц情女王★ 提交于 2019-12-11 07:34:31

问题


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

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.

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

code of user.py file

from pathlib import Path

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

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

code of conftest.py

from packagename.user import func

func()

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

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.


回答1:


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

Try:

  • cleanup your development virtualenv or delete it and create a new one.
  • cd the/root/of/your/source/tree
  • pip install -e .

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



来源:https://stackoverflow.com/questions/56461650/pathlib-path-resolves-installed-path-directory-of-package-instead-of-source-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!