有没有像Makefile一样方便的工具可以将路径追加到PYTHONPATH?

huangapple go评论62阅读模式
英文:

Is there a convinient tool like Makefile for Python that will append a path to PYTHONPATH?

问题

我有一个问题,我想要将一个路径添加到PYTHONPATH。当然,我不想修改我的.bashrc,只是为了让这个项目正常运行。

例如,PyCharm会在创建配置时将根目录内容添加到PYTHONPATH,因此当你在PyCharm内部运行程序时,它会正常工作。

我想知道是否有一个类似于Python的Makefile的配置程序,你可以定义配置,比如“运行这个脚本,但也将当前路径附加到PYTHONPATH”。就像PyCharm所做的那样,但是用于命令行的等效方式。

英文:

I have an issue where I want to add a path to PYTHONPATH. Of course, I don't want to modify my .bashrc to get just this project running.

PyCharm for example will add to PYTHONPATH the root contents when you create a configuration and as a result when you run the program from within PyCharm it will work.

I was wondering if there is a configuration program like Makefile for Python that you can define configurations like "run this script but also append the current path to PYTHONPATH". An equivalant of what PyCharm does but for the command line.

答案1

得分: 1

你想要做的是使用一个存在于你的Python's site-packages目录之外的包,这可以在不修改 PYTHONPATH 的情况下完成,pip可以通过以可编辑模式安装来实现这一点(这将把它添加到Python的.pth文件中)。

pip install -e folder_containing_setup_dot_py

一个用于简单脚本文件夹的最小setup.py将如下所示:

# setup.py

from setuptools import setup

setup(package_dir={'': 'src'}, packages=['package_name'])

你的项目应该如下所示:

.
└── 项目的根目录/
    ├── setup.py
    └── src/
        └── package_name/
            ├── package_file.py
            └── __init__.py

然后,你可以从任何地方简单地导入你的代码:

from package_name import package_file
英文:

what you want to do is to use a package that exists outside of your python's site-packages directory, this can be done without modifying PYTHONPATH , pip has the ability to do that by installing it in editable mode, (which adds it to python's .pth file)

pip install -e folder_containing_setup_dot_py

a minimal setup.py would look as follows for a simple script folder.

# setup.py

from setuptools import setup

setup(package_dir={'':'src'},packages=['package_name'])

and your project should look as follows:

.
└── root directory of project/
    ├── setup.py
    └── src/
        └── package_name/
            ├── package_file.py
            └── __init__.py

and you could simply import your code from anywhere

from package_name import package_file

huangapple
  • 本文由 发表于 2023年4月7日 01:26:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75952237.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定