1

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?
CiaranWelsh
  • 7,014
  • 10
  • 53
  • 106
  • Could you explain in more detail why it's not importable using external modules? – OneCricketeer Jun 18 '17 at 18:34
  • Yes, because my developing package lives under `C://Documents/myPackage` which is not in the `PythonPath` variable – CiaranWelsh Jun 18 '17 at 18:39
  • So? You can still import it https://stackoverflow.com/questions/67631/how-to-import-a-module-given-the-full-path – OneCricketeer Jun 18 '17 at 18:41
  • Ah yes, I tried this one. I didn't continue with this approach because my package contains many modules and I get unresolved imports from within the package (hopefully that makes sense). I suppose I could add all the modules in the package using a loop. Thanks for the advice. – CiaranWelsh Jun 18 '17 at 18:44

1 Answers1

2

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

ScottK
  • 1,526
  • 1
  • 16
  • 23