简体   繁体   中英

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).

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.

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.

So my question is how do other people deal with this problem?

  • Do you create and develop code directly in the site-packages directory?
  • Would you add the 'Documents/myPackage' path temporarily (or permanently) to the PATH variable?
  • 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. You can create new virtual environments for python with their own tool versions and path variables. 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. Install pip first using the instructions here .

sudo apt-get install python-pip

Then install virtualEnv:

 pip install virtualenv

Read the user guide for virtualEnv at the Hitchhiker's Guide to Python

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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