importing from another folder in virtualenv
<p>I'm following the <a href="http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world" rel="nofollow">Flask Mega Tutorial</a>, and I'm running into an issue once I get to the second part and restructure my folder structure to match theirs, I cannot import Flask.</p>

<p>My current folder structure is as follows</p>

<pre><code>/FlaskTest
    /app
        /static, templates etc
    /flask
        /virtualenv folders etc
    /tmp
    run.py
</code></pre>

<p>as far as I can tell, the folder structures are identical other than naming of the top level directory.</p>

<p>in my __init__.py file (/app/__init__.py), I'm doing as instructed in the tutorial, </p>

<pre><code>from flask import Flask

app = Flask(__name__)
from app import views
</code></pre>

<p>I'm getting an Import Error that "cannot import name 'Flask'". I'm guessing the issue is because the flask package was installed to /flask/lib/site-packages.</p>

<p>My question: How can I reference the sub folder of flask/site-packages?</p>

<p>I've read through the python  <a href="https://docs.python.org/3/reference/import.html" rel="nofollow">import system documentation</a> and from what I can make of it through the first pass of reading it over, I would need to likely do something like from flask import flask.Flask or something to that effect.</p>

<p><strong>UPDATE</strong>: So after cd'ing around the directory and checking pip list, I realized that flask wasn't accessible to my app directory. I ran pip install flask in the app directory. Now my site runs, but I'm not sure if this is the best practice of doing things with Python. Please provide some clarity as what the best practice is for installing packages and where the packages reside.</p>

<p><strong>UPDATE 2</strong>: After creating a directory called standalone. In this folder, I created a virtual environment called standalone-test. Once, I did that, I also mkdir'ed app and copied it's contents from FlaskTest so that way the code would be identical. I was able to run the run.py script by using <code>python run.py</code>, but I can't run <code>python -m app</code> like you had said without running into an error. The error is as follows if it helps.</p>

<p>"No module name app.<strong>main</strong>; 'app' is a package and cannot be directly executed. </p>

<p>I am able to run <code>python run.py</code> as I mentioned, but I'm not able to run the <code>python -m app</code> command as you had mentioned</p>

<p>I think something went wrong in your execution environment. Here are some explanations.</p>

<h3>The virtualenv</h3>

<p>See the documentation of <a href="https://virtualenv.pypa.io/en/stable/userguide/" rel="nofollow">virtualenv</a></p>

<p>If you have followed the tutorial:</p>

<ul>
<li>The <code>flask</code> directory is your virtualenv,</li>
<li>On posix system, you have a <code>flask/bin</code> subdirectory, or</li>
<li>On Windows system, you have a <code>flask\Scripts</code> subdirectory.</li>
</ul>

<p>I make the assumption that you are on posix system.</p>

<p>To activate your virtualenv, run:</p>

<pre><code>source flask/bin/activate
</code></pre>

<p>Your prompt should change to something like: <code>(flask)$</code>. </p>

<p>To list the installed libraries use <code>pip</code>:</p>

<pre><code>pip list
</code></pre>

<p>Make sure you see Flask. The tutorial encourages you to install a lot of Flask plugins, so there are a lot of Flask-Somethingâ¦</p>

<p>If Flask is missing, install it:</p>

<pre><code>pip install Flask
</code></pre>

<h3>Run your app</h3>

<p>Your application is in the <code>app</code> directory, it has an <code>__init__.py</code> file (it's a Python package).</p>

<p>In this file, you have:</p>

<pre><code>from flask import Flask

app = Flask(__name__)
from app import views
</code></pre>

<p>From your <code>FlaskTest/</code> directory, try to run this script like this:</p>

<pre><code>cd FlaskTest/  # if not in this directory
python -m app
</code></pre>

<p>This should import <code>Flask</code>, instanciate your <code>app</code> (but don't run it), import the <code>views</code> module.</p>

<p>If <code>app/views.py</code> exist you should have no error.</p>

<p>=> at this point, we have simulated what <code>run.py</code> importsâ¦</p>

<p>Now write <code>run.py</code> in your <code>FlaskTest/</code> directory:</p>

<pre><code>#!flask/bin/python
from app import app
app.run(debug=True)
</code></pre>

<p>Run it like this:</p>

<pre><code>python run.py
</code></pre>

<p>Note that the shebang <code>#!flask/bin/python</code> is unusual, but should work in the context of the tutorial.</p>

<p>This should start your http serverâ¦</p>

python flask pip virtualenv