英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论