[英]Developing a Python package under a local directory, from site-packages folder or is there a better option?
I'm writing a Python package and I have the code in my Documents
folder under a subfolder named myPackage
(for sake of argument). 我正在编写一个Python包,并且我的
Documents
文件夹中的代码位于名为myPackage
的子Documents
夹下(出于争论)。
Whilst developing the modules in this package I've been writing test code at the bottom of the script and running/adding/modifying as the code develops. 在开发此软件包中的模块时,我一直在脚本底部编写测试代码,并在代码开发时运行/添加/修改。 I essentially now want to test my code from outside the package now (because of a weird bug that would be essentially impossible to explain) by writing other scripts that use myPackage in the way that its intended to be run.
现在,我基本上现在想通过编写其他按预期运行方式使用myPackage的脚本来从程序包外部测试我的代码(由于从根本上无法解释的怪异错误)。
The problem is that the code is not importable from its current location and I don't want to add it to the PythonPath
variable because this will cause namespace clashes down the line, after installing in the site-packages directory using PyPI. 问题是该代码无法从当前位置导入,并且我不想将其添加到
PythonPath
变量中,因为在使用PyPI安装到site-packages目录中后,这将导致名称空间冲突。
So my question is how do other people deal with this problem? 所以我的问题是其他人如何处理这个问题?
Check out Python VirtualEnv , it does exactly what I think you want. 查看Python VirtualEnv ,它确实满足我的要求。 You can create new virtual environments for python with their own tool versions and path variables.
您可以使用自己的工具版本和路径变量为python创建新的虚拟环境。 Any changes made to that environment, stay in that environment.
对该环境所做的任何更改均保留在该环境中。 You can activate a virtual environment as desired, then deactivate it to go back to your native environment, or switch to another virtual environment.
您可以根据需要激活虚拟环境,然后将其停用以返回到本机环境,或切换到另一个虚拟环境。
To install it, most Python developers use pip. 要安装它,大多数Python开发人员都使用pip。 Install pip first using the instructions here .
首先按照此处的说明安装pip。
sudo apt-get install python-pip
Then install virtualEnv: 然后安装virtualEnv:
pip install virtualenv
Read the user guide for virtualEnv at the Hitchhiker's Guide to Python 阅读《 Hitchhiker Python 指南》中的virtualEnv用户指南。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.