Reputation: 15328
When working with environment vars, a .env
file is used for development via python-dotenv. When the app is distributed (and so installed using setuptools
), the env vars are provided by an other way of course.
The problem is after installing in a virtual env via setup.py install
, launching a script that uses load_dotenv
raises the error OSError: Starting path not found:
Traceback (most recent call last):
File "/home/dev/testpkg/venv/bin/run", line 11, in <module>
load_entry_point('testpkg==0.0.1', 'console_scripts', 'run')()
File "/home/dev/testpkg/venv/lib/python3.8/site-packages/pkg_resources/__init__.py", line 489, in load_entry_point
return get_distribution(dist).load_entry_point(group, name)
File "/home/dev/testpkg/venv/lib/python3.8/site-packages/pkg_resources/__init__.py", line 2852, in load_entry_point
return ep.load()
File "/home/dev/testpkg/venv/lib/python3.8/site-packages/pkg_resources/__init__.py", line 2443, in load
return self.resolve()
File "/home/dev/testpkg/venv/lib/python3.8/site-packages/pkg_resources/__init__.py", line 2449, in resolve
module = __import__(self.module_name, fromlist=['__name__'], level=0)
File "/home/dev/testpkg/venv/bin/run.py", line 4, in <module>
__import__('pkg_resources').run_script('testpkg==0.0.1', 'run.py')
File "/home/dev/testpkg/venv/lib/python3.8/site-packages/pkg_resources/__init__.py", line 666, in run_script
self.require(requires)[0].run_script(script_name, ns)
File "/home/dev/testpkg/venv/lib/python3.8/site-packages/pkg_resources/__init__.py", line 1469, in run_script
exec(script_code, namespace, namespace)
File "/home/dev/testpkg/venv/lib/python3.8/site-packages/testpkg-0.0.1-py3.8.egg/EGG-INFO/scripts/run.py", line 7, in <module>
File "/home/dev/testpkg/venv/lib/python3.8/site-packages/dotenv/main.py", line 336, in load_dotenv
dotenv_path = find_dotenv()
File "/home/dev/testpkg/venv/lib/python3.8/site-packages/dotenv/main.py", line 300, in find_dotenv
for dirname in _walk_to_root(path):
File "/home/dev/testpkg/venv/lib/python3.8/site-packages/dotenv/main.py", line 257, in _walk_to_root
raise IOError('Starting path not found')
OSError: Starting path not found
The solution to this problem would be to use pip install .
which works fine.
I know that eggs are deprecated, but I would like to know if there is a solution to make this work using setup.py install
?
There's a minimal project to reproduce the error.
A fresh project installed in a virtual environment using Python 3.8 and a src-layout model.
root_project_dir/
|--- .env
|--- setup.py
|--- src/
|--- run.py
|--- pkg/
|--- __init__.py
|--- mod.py
Of course the .env
is only for local testing and is not aimed to be distributed.
The run.py
script's main()
function is the entry point of the app.
setup.py
contentfrom setuptools import setup, find_namespace_packages
setup(
name='testpkg',
version='0.0.1',
install_requires=["python-dotenv"],
packages=find_namespace_packages(where='src'),
package_dir={"": "src"},
scripts=['src/run.py'],
entry_points={
'console_scripts': [
'run = run:main',
]
}
)
run.py
contentimport os
from dotenv import load_dotenv
from pkg.mod import Mod
load_dotenv()
VAR = os.getenv('VAR')
def main():
m = Mod(VAR)
m.run()
if __name__ == "__main__":
main()
mod.py
contentclass Mod(object):
def __init__(self, var):
print("init with var:", var)
def run(self):
print("Ok I ran!")
.env
contentVAR=some text
Create and activate a virtual env:
python3 -m venv venv
source ./venv/bin/activate
Run locally works with the .env retrieved:
(venv) root_project_dir/$ pip install python-dotenv
(venv) root_project_dir/$ python src/run.py
init with var: some text
Ok I ran!
First install:
(venv) root_project_dir/$ python setup.py install
...
Using /home/dev/testpkg/venv/lib/python3.8/site-packages
Finished processing dependencies for testpkg==0.0.1
Then run the entry point generates the error:
(venv) root_project_dir/$ run
Traceback (most recent call last):
File "/home/dev/testpkg/venv/bin/run", line 11, in <module>
...
File "/home/dev/testpkg/venv/lib/python3.8/site-packages/dotenv/main.py", line 300, in find_dotenv
for dirname in _walk_to_root(path):
File "/home/dev/testpkg/venv/lib/python3.8/site-packages/dotenv/main.py", line 257, in _walk_to_root
raise IOError('Starting path not found')
OSError: Starting path not found
At line 300, path
value is /home/dev/testpkg/venv/lib/python3.8/site-packages/testpkg-0.0.1-py3.8.egg/EGG-INFO/scripts
whereas /home/dev/testpkg/venv/lib/python3.8/site-packages/testpkg-0.0.1-py3.8.egg
is of course a file.
Again, the solution to this problem would be to use pip install .
which works fine.
I know that eggs are deprecated, but I would like to know if there is a solution to make this work using setup.py install
?
Upvotes: 0
Views: 1307
Reputation: 353
Removing .pytest_cache
and __pycache__
folders might help. The problem occured to me after I renamed parent folder.
Upvotes: 1
Reputation: 15328
If other users have the same issue and just want to make it working,
as I said, a way to solve this problem is to use pip instead of python setup.py install
:
(venv) pip install .
And voilà!
As you can see in this answer, eggs are deprecated and replaced by using pip/wheels.
Upvotes: 0