3

I wrote a small python program and again I am struggling with producing a good structure.

A few days ago I read a blog and many other websites, which advise against from a import b imports and recommend to use always import a.b "project absolute" path (the whole path from the project root to what I want to import) imports. That as a background.

I included a __main__.py so that I can run my program with python -m <directory>, which was another recommendation of someone on stackoverflow in one of the hundreds of python import questions. It is supposed to help keeping code runnable and testable with the same import structure, which was a problem in another project of mine.

Now what I want it, that from anywhere in my system, I can run python -m <dir of my code> and not only from one directory up the RSTCitations directory.

How can I achieve that, without:

  • python path manipulations (which are a dirty hack)
  • changing my imports somehow and getting a not recommended import structure
  • doing other dark magic to my code

I want to stick to best practices in organizing my code but still want it to be runnable from wherever I am in the terminal.

Example of fail

When I run the program as described from another directory completely unrelated to my program, I get for example the following error:

/home/user/development/anaconda3/bin/python: No module named /home/user/development/rst-citations-to-raw-latex/RSTCitations

However the path is correct. That is exactly where the code is.

Zelphir Kaltstahl
  • 5,722
  • 10
  • 57
  • 86
  • There's nothing wrong with `from a import b` (and indeed the blog you reference does *not* recommend against it). – Andrew Jaffe Nov 25 '16 at 14:13
  • have you tried installing your package in pip ? – Alex Garcia Nov 25 '16 at 14:14
  • @AlexGarcia No. I don't want users to have to install anything (maybe in the future I'll upload it there, although I've never done that and no clue how to go about it and what steps are necessary), just be able to run my progam from anywhere. – Zelphir Kaltstahl Nov 25 '16 at 14:15
  • @AndrewJaffe You are right, seems I mixed up the websites. I somewhere read that though, that one should always use `import ...` Sorry that I cannot find it anymore : ( – Zelphir Kaltstahl Nov 25 '16 at 14:17
  • @Zelphir you can install a 'local' package with pip (look over here : http://stackoverflow.com/questions/15031694/installing-python-packages-from-local-file-system-folder-with-pip ) – Alex Garcia Nov 25 '16 at 14:17
  • @AlexGarcia Is this the only way? I mean, I can run my program just fine ... only that I have to be in a specific location to do so, which is an annoyance. Having to use yet another tool just to be able to then use my program seems a bit odd. If you are sure that this is the only way I can achieve what I want, you could make this an answer. – Zelphir Kaltstahl Nov 25 '16 at 14:19

1 Answers1

0

You can :

If you don't need other users to 'import' your library but just use it as a standalone program, you can also just put a symlink/script to your program, makeing it runnable from a directory which is in your PATH.

Community
  • 1
  • 1
Alex Garcia
  • 773
  • 7
  • 21
  • Can you help me with the symlink idea? Do you mean I should create a symlink to a script which runs my program and lies inside the directory from which I at the moment have to call my program? – Zelphir Kaltstahl Nov 25 '16 at 14:41