Repository: deepset-ai/haystack

Issue:
Haystack Version undefined when using source code
**Describe the bug**
When cloning and using this repository (without installing `haystack-ai` package in venv), calling `from haystack import Pipeline` raises a `PackageNotFoundError`

**Error message**
```
---------------------------------------------------------------------------
PackageNotFoundError                      Traceback (most recent call last)
File /media/max/3tbdrive/PycharmProjects/haystack/examples/../haystack/version.py:11
     10 try:
---> 11     __version__ = str(metadata.version("haystack-ai"))
     12 except metadata.PackageNotFoundError:

File /usr/lib/python3.10/importlib/metadata/__init__.py:996, in version(distribution_name)
    990 """Get the version string for the named package.
    991 
    992 :param distribution_name: The name of the distribution package to query.
    993 :return: The version string for the package as defined in the package's
    994     "Version" metadata key.
    995 """
--> 996 return distribution(distribution_name).version

File /usr/lib/python3.10/importlib/metadata/__init__.py:969, in distribution(distribution_name)
    964 """Get the ``Distribution`` instance for the named package.
    965 
    966 :param distribution_name: The name of the distribution package as a string.
    967 :return: A ``Distribution`` instance (or subclass thereof).
    968 """
--> 969 return Distribution.from_name(distribution_name)

File /usr/lib/python3.10/importlib/metadata/__init__.py:548, in Distribution.from_name(cls, name)
    547 else:
--> 548     raise PackageNotFoundError(name)

PackageNotFoundError: No package metadata was found for haystack-ai

During handling of the above exception, another exception occurred:

PackageNotFoundError                      Traceback (most recent call last)
Cell In[2], line 7
      3 from typing import Optional, Any
      5 from huggingface_hub import HfApi
----> 7 from haystack import Pipeline
      8 from haystack.components.builders.answer_builder import AnswerBuilder
      9 from haystack.components.builders.prompt_builder import PromptBuilder

File /media/max/3tbdrive/PycharmProjects/haystack/examples/../haystack/__init__.py:4
      2 from haystack.core.serialization import default_from_dict, default_to_dict
      3 from haystack.core.errors import DeserializationError, ComponentError
----> 4 from haystack.pipeline import Pipeline
      5 from haystack.dataclasses import Document, Answer, GeneratedAnswer, ExtractedAnswer
      8 __all__ = [
      9     "component",
     10     "default_from_dict",
   (...)
     18     "ExtractedAnswer",
     19 ]

File /media/max/3tbdrive/PycharmProjects/haystack/examples/../haystack/pipeline.py:8
      5 import logging
      7 from haystack.core.pipeline import Pipeline as _pipeline
----> 8 from haystack.telemetry import pipeline_running
      9 from haystack.marshal import Marshaller, YamlMarshaller
     12 DEFAULT_MARSHALLER = YamlMarshaller()

File /media/max/3tbdrive/PycharmProjects/haystack/examples/../haystack/telemetry/__init__.py:1
----> 1 from haystack.telemetry._telemetry import pipeline_running, tutorial_running

File /media/max/3tbdrive/PycharmProjects/haystack/examples/../haystack/telemetry/_telemetry.py:11
      8 import yaml
      9 import posthog
---> 11 from haystack.telemetry._environment import collect_system_specs
     13 if TYPE_CHECKING:
     14     from haystack.pipeline import Pipeline

File /media/max/3tbdrive/PycharmProjects/haystack/examples/../haystack/telemetry/_environment.py:8
      5 import sys
      6 from typing import Optional, Dict, Any
----> 8 from haystack.version import __version__
     10 logger = logging.getLogger(__name__)
     13 # This value cannot change during the lifetime of the process

File /media/max/3tbdrive/PycharmProjects/haystack/examples/../haystack/version.py:13
     11     __version__ = str(metadata.version("haystack-ai"))
     12 except metadata.PackageNotFoundError:
---> 13     __version__ = str(metadata.version("farm-haystack"))

File /usr/lib/python3.10/importlib/metadata/__init__.py:996, in version(distribution_name)
    989 def version(distribution_name):
    990     """Get the version string for the named package.
    991 
    992     :param distribution_name: The name of the distribution package to query.
    993     :return: The version string for the package as defined in the package's
    994         "Version" metadata key.
    995     """
--> 996     return distribution(distribution_name).version

File /usr/lib/python3.10/importlib/metadata/__init__.py:969, in distribution(distribution_name)
    963 def distribution(distribution_name):
    964     """Get the ``Distribution`` instance for the named package.
    965 
    966     :param distribution_name: The name of the distribution package as a string.
    967     :return: A ``Distribution`` instance (or subclass thereof).
    968     """
--> 969     return Distribution.from_name(distribution_name)

File /usr/lib/python3.10/importlib/metadata/__init__.py:548, in Distribution.from_name(cls, name)
    546         return dist
    547 else:
--> 548     raise PackageNotFoundError(name)

PackageNotFoundError: No package metadata was found for farm-haystack
```


**Expected behavior**
I'm able to use haystack repo in a standalone fashion. This ensures that I won't accidentally import the site-packages code of the PyPy version of this repo.

**Additional context**
-

**To Reproduce**
1. git clone this repository
2. Install dependencies
3. Ensure `farm-haystack` and `haystack-ai` are not installed as dependencies
4. call  `from haystack import Pipeline` within the root directory (or use `sys.path.append` to add the root folder)

**FAQ Check**
- [X] Have you had a look at [our new FAQ page](https://docs.haystack.deepset.ai/docs/faq)?

**System:**
 - OS: Ubuntu 20.04
 - GPU/CPU: GPU
 - Haystack version (commit or version number): [eaec5bf](https://github.com/deepset-ai/haystack/commit/eaec5bfe4ab93503d17ccbe4e2e44c5c3ae5a857)
 - DocumentStore: NA
 - Reader: NA
 - Retriever: NA



Discussion:
Hey, @maxjeblick!

 To locally install Haystack from the main branch, I would do this:
```bash
git clone https://github.com/deepset-ai/haystack
cd haystack
pip install .
```

This works (and does not install haystack from PyPi)

But maybe I am missing what you are trying to do. Please explain...
Hi @anakin87, `pip install .` will install `haystack-ai` package as a dependency (at least when I checked).
It can be confirmed by inspecting the site-packages folder. When you run `pip uninstall haystack-ai`, you will be able to reproduce the error.

To follow up on this, the problem I see when having `haystack-ai` as a dependency is that it is very easy to accidentally import the code from the site package folder instead of the cloned repository folder. I've hit this issue various times on several projects when trying to change the code, to find out that I'm actually not importing my changed code, but the fixed one from site-package.

This is an example when running within a python console:
```
import haystack
import inspect
print(inspect.getfile(haystack))
/media/max/3tbdrive/PycharmProjects/haystack/venv/lib/python3.10/site-packages/haystack/__init__.py
```
I uninstalled  `haystack-ai` dependency and manually set a version, I get (on a new session):
```
import haystack
import inspect
print(inspect.getfile(haystack))
/media/max/3tbdrive/PycharmProjects/haystack/haystack/__init__.py
```






Resolving patch:
diff --git a/haystack/version.py b/haystack/version.py
--- a/haystack/version.py
+++ b/haystack/version.py
@@ -1,13 +1,6 @@
 from importlib import metadata
 
-# haystack is distributed as a separate package called `haystack-ai`.
-# We want to keep all preview dependencies separate from the current Haystack version,
-# so imports in haystack must only import from haystack.
-# Since we need to access __version__ in haystack without importing from
-# haystack we must set it here too.
-# When installing `haystack-ai` we want to use that package version though
-# as `farm-haystack` might not be installed and cause this to fail.
 try:
     __version__ = str(metadata.version("haystack-ai"))
 except metadata.PackageNotFoundError:
-    __version__ = str(metadata.version("farm-haystack"))
+    __version__ = "main"


Test patch: