0

I have my github project MYMODULE structure following the guidelines e.g.:

README.rst
LICENSE
sample/__init__.py
sample/core.py
sample/helpers.py

The core of my project is inside sample folder where __init__.py is stored. I would like to be able to keep it accessible including sample folder in the PYTHONPATH, but of course if I git -clone the project in a folder listed in the PYTHONPATH I will have another folder MYMODULE before the sample folder with the __init__ file, and so I won't be able to import it. I know that if I move the sample folder one level up I will be able to access the module but this will compromise the sync with my GitHub repository.

Is there any guideline or best practice for solving this issue?

I've tried to use git sparse checkout but this doesn't solve the issue because it however stores the folder selected inside a parent folder.

G M
  • 20,759
  • 10
  • 81
  • 84
  • How about cloning into current folder? https://stackoverflow.com/a/46914108/5588279 – Sraw May 18 '19 at 08:39
  • `git` is a development tool but not distribution/deployment. To install a Python package you should have [`setup.py`](https://packaging.python.org/). Then you can install the package [directly from git](https://pip.readthedocs.io/en/stable/reference/pip_install/#vcs-support) using [`pip`](https://pip.readthedocs.io/en/stable/user_guide/) or you can clone the repo yourself and install with `pip install -e .` or even with `python setup.py install`. – phd May 18 '19 at 12:24
  • @Sraw that will clone all the project and I will have the same result – G M May 18 '19 at 12:50
  • @phd if fact I am using git as a development tool and I want to develop my project inside the python path to be able to import it outside the project folder, I don't want to make a setup.py for a project in the earl phases – G M May 18 '19 at 12:52
  • During development point `$PYTHONPATH` to the current directory: `export PYTHONPATH=$(pwd)` – phd May 18 '19 at 12:53
  • @phd thanks but so I have to add a directory to export a for every project I do? I would prefer to avoid creating so many system variables... I am not sure is a best practice – G M May 18 '19 at 12:58
  • The best practice is [tag:virtualenv]. Create `setup.py` for every package, create a virtualnev for every separate project, install projects with `pip install -e .` – phd May 18 '19 at 13:50
  • @phd thanks a lot I've tried your solution with pip install -e I think is the only valid, can you formalize it with an answer for other users – G M May 18 '19 at 13:54

1 Answers1

1

git is a development tool but not distribution/deployment. To install a Python package you should have setup.py. Then you can install the package directly from git using pip or you can clone the repository yourself and install with pip install -e . or even with python setup.py install

During development you can clone the code and point $PYTHONPATH to the current directory: export PYTHONPATH=$(pwd)

But the best practice is virtualenv. Create setup.py for every package, create a virtualenv for every separate project, install packages with pip install -e .

phd
  • 82,685
  • 13
  • 120
  • 165