Python package without installation

As part of my build system, I use a modified version of the Python package (cogapp). I do not want to install the package because:

  • I changed the package and do not want to worry about colliding with unmodified versions that may already be installed.
  • It is better if users of the build system do not need to install additional packages.

However, I am having problems using the package if it is not installed. If it is installed I can run:

python -m cogapp <additional args>

and everything works as intended.

There is a __main__.py script in the package:

 import sys from cogapp import Cog sys.exit(Cog().main(sys.argv)) 

I tried running this directly, for example:

python -m <path>/__main__ <additional_args>

But I get the error:

 ... /__main__.py", line 3, in <module> from cogapp import Cog ImportError: No module named cogapp 

This is probably due to the error I get if I run __init__.py :

from .cogapp import *

Error:

  from .cogapp import * ValueError: Attempted relative import in non-package 

How can I run a package as a package?

EDIT

I found a fix by removing all relative imports from cogapp and removing -m, i.e. not working as a module. In this case, this is not so bad because it is a small package with a single directory. However, I am interested in how this should be done in the future. There is a lot of material written around this topic, but there are no clear answers!

+4
source share
1 answer

Here is the solution I came to.

Disclaimer: you are actually installing the package, but on a different path than the standard one.

 $ mkdir newhome $ python setup.py install --home=./newhome $ PYTHONPATH=$PWD/newhome/lib/python <COMMAND_NEEDING_THAT_PACKAGE> 
+2
source

Source: https://habr.com/ru/post/1499146/


All Articles