title: Calling script in standard project directory structure (Python path for bin subdirectory)
body: <p>I am experimenting with putting my Python code into the standard directory structure used for deployment with <code>setup.py</code> and maybe PyPI. for a Python library called mylib it would be something like this:</p>
  
  <pre><code>mylibsrc/
    README.rst
    setup.py
    bin/
      some_script.py
    mylib/
      __init.py__
      foo.py
  </code></pre>
  
  <p>There's often also a <code>test/</code> subdirectory but I haven't tried writing unit tests yet. The recommendation to have scripts in a <code>bin/</code> subdirectory can be found in the <a href="http://python-packaging.readthedocs.io/en/latest/command-line-scripts.html" rel="noreferrer">official Python packaging documentation</a>. </p>
  
  <p>Of course, the scripts start with code that looks like this:</p>
  
  <pre><code>#!/usr/bin/env python
  from mylib.foo import something
  something("bar")
  </code></pre>
  
  <p>This works well when it eventually comes to deploying the script (e.g. to devpi) and then installing it with pip. But if I run the script directly from the source directory, as I would while developing new changes to the library/script, I get this error:</p>
  
  <pre><code>ImportError: No module named 'mylib'
  </code></pre>
  
  <p>This is true even if the current working directory is the root <code>mylibsrc/</code> and I ran the script by typing <code>./bin/some_script.py</code>. This is because Python starts searching for packages in the directory of the script being run (i.e. from <code>bin/</code>), not the current working directory.</p>
  
  <p>What is a good, permament way to make it easy to run scripts while developing packages? </p>
  
  <p>Here is a <a href="https://stackoverflow.com/questions/193161/what-is-the-best-project-structure-for-a-python-application">relevant other question</a> (especially comments to the first answer).</p>
  
  <p>The solutions for this that I've found so far fall into three categories, but none of them are ideal:</p>
  
  <ol>
  <li>Manually fix up your Python's module search path somehow before running your scripts.
  
  <ul>
  <li>You can manually add <code>mylibsrc</code> to my <code>PYTHONPATH</code> environment variable. This seems to be the most official (Pythonic?) solution, but means that every time I check out a project I have to remember to manually change my environment before I can run any code in it.</li>
  <li>Add <code>.</code> to the start of my <code>PYTHONPATH</code> environment variable. As I understand it this could have some security problems. This would actually be my favoured trick if I was the only person to use my code, but I'm not, and I don't want to ask others to do this.</li>
  <li>While looking at answers on the internet, for files in a <code>test/</code> directory I've seen recommendations that they all (indirectly) include a line of code <code>sys.path.insert(0, os.path.abspath('..'))</code> (e.g. in <a href="https://stackoverflow.com/questions/193161/what-is-the-best-project-structure-for-a-python-application">structuring your project</a>). Yuck! This seems like a bearable hack for files that are only for testing, but not those that will be installed with the package.</li>
  <li>Edit: I have since found an alternative, which turns out to be in this category: by running the scripts with Python's <code>-m</code> script, the search path starts in the working directory instead of the <code>bin/</code> directory. See my answer below for more details.</li>
  </ul></li>
  <li>Install the package to a virtual environment before using it, using a setup.py (either running it directly or using pip).
  
  <ul>
  <li>This seems like overkill if I'm just testing a change that I'm not sure is even syntactically correct yet. Some of the projects I'm working on aren't even meant to be installed as packages but I want to use the same directory structure for everything, and this would mean writing a setup.py just so I could test them!</li>
  <li>Edit: Two interesting variants of this are discussed in the answers below: the <code>setup.py develop</code> command in logc's answer and <code>pip install -e</code> in mine. They avoid having to re-"install" for every little edit, but you still need to create a <code>setup.py</code> for packages you never intend to fully install, and doesn't work very well with PyCharm (which has a menu entry to run the <code>develop</code> command but no easy way to run the scripts that it copies to the virtual environment).</li>
  </ul></li>
  <li>Move the scripts to the project's root directory (i.e. in <code>mylibsrc/</code> instead of <code>mylibsrc/bin/</code>).
  
  <ul>
  <li>Yuck! This is a last resort <strike>but, unfortunately, this seems like the only feasible option at the moment</strike>.</li>
  </ul></li>
  </ol>