0

I've been trying to execute django-admin from within a LiClipse project using:

projectName = "someProject"
command = 'django-admin startproject ' + projectName
process = subprocess.Popen(command.split(), stdout=subprocess.PIPE)

and also

subprocess.check_call(shlex.split(command))

But each time I get the error:

FileNotFoundError: [Errno 2] No such file or directory: 'django-admin'

However, when I run the same program from the Linux terminal using python3 main.py, it works fine.

So I figured it might be because django-admin's path isn't added to PYTHONPATH. I did a locate "django-admin" to find these paths:

/home/nav/.pyenv/shims/django-admin
/home/nav/.pyenv/shims/django-admin.py
/home/nav/.pyenv/versions/3.8.7/bin/django-admin
/home/nav/.pyenv/versions/3.8.7/bin/django-admin.py
/home/nav/.pyenv/versions/3.8.7/bin/__pycache__/django-admin.cpython-38.pyc
/home/nav/.pyenv/versions/3.8.7/lib/python3.8/site-packages/django/bin/django-admin.py
/home/nav/.pyenv/versions/3.8.7/lib/python3.8/site-packages/django/bin/__pycache__/django-admin.cpython-38.pyc

and added it to PYTHONPATH... enter image description here

...but I get the same error when I run the program from within LiClipse.

Does anyone know why this problem is happening and how it can be fixed?

Nav
  • 16,995
  • 26
  • 78
  • 120
  • Well, you added an interpreter, but not something to your shell PATH. Normally this is handled by the virtualenv's "activate" command and IDE's have support for virtual environments. If Eclipse doesn't have that, it means you're going to have provide absolute paths to the `django-admin` command you want executed or you properly setup pyenv so that Eclipse doesn't need additional configuration. The shims are already there. – Melvyn Apr 25 '21 at 13:06
  • Tried adding `export PATH="/home/nav/.pyenv/versions/3.8.7/bin/:$PATH"` to ~/.bashrc. Restarted computer. Am getting the same `FileNotFoundError: [Errno 2] No such file or directory: 'django-admin'` error. – Nav Apr 25 '21 at 16:30
  • Does that work in your shell now? Then Eclipse must somewhere be using a sanitized / hardcoded path. Unfortunately, I'm not familiar with the Eclipse itself. If it doesn't work in your shell either, then either you're not using bash as a shell or somewhere later in the bash startup chain, PATH is overridden again. – Melvyn Apr 25 '21 at 17:05
  • I had already mentioned that the program works fine when I run it from the shell using `python3 main.py` – Nav Apr 26 '21 at 05:01
  • Then `print(os.environ['PATH'])` before invoking subprocess. Compare the difference when invoked from Eclipse versus the command line. – Melvyn Apr 26 '21 at 16:55
  • Aha! Great suggestion. When running from the terminal, it's showing a whole bunch of extra folders as part of PATH. All of these folders are subfolders of `/home/nav/.pyenv/`. Thanks! :-) – Nav Apr 27 '21 at 19:19
  • So that's caused by PATH being altered after [dbus](https://www.freedesktop.org/wiki/Software/dbus/) sets it. Then there's a discrepancy between shell PATH and the PATH that distribution programs have. This generally is a good thing, as software assumes certain version of a interpreter or program to run scripts and when a different version is used due to user-set PATH mangling, it can lead to hard to diagnose bugs. – Melvyn Apr 28 '21 at 06:17
  • Really? dbus sets it? I have my doubts. Anyway, even after exporting the pyenv paths in bashrc and even after adding the pyenv paths in LiClipse's Preferences > Linked resources, when I start the program in LiClipse, none of the pyenv paths are output by `os.environ`. I'm back to square one. – Nav Apr 28 '21 at 07:26
  • and although I've found a way to add it to os.environ path, a generic solution would be better. https://stackoverflow.com/questions/1681208/python-platform-independent-way-to-modify-path-environment-variable – Nav Apr 28 '21 at 07:41

0 Answers0