简体   繁体   English

从site-packages文件夹在本地目录下开发Python软件包,还是有更好的选择?

[英]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? 所以我的问题是其他人如何处理这个问题?

  • Do you create and develop code directly in the site-packages directory? 您是否直接在site-packages目录中创建和开发代码?
  • Would you add the 'Documents/myPackage' path temporarily (or permanently) to the PATH variable? 您是否将“文档/ myPackage”路径临时(或永久)添加到PATH变量?
  • Are there any packages specifically designed to deal with this type of 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM