简体   繁体   中英

importing from another folder in virtualenv

I'm following the Flask Mega Tutorial , 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.

My current folder structure is as follows

/FlaskTest
    /app
        /static, templates etc
    /flask
        /virtualenv folders etc
    /tmp
    run.py

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

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

from flask import Flask

app = Flask(__name__)
from app import views

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.

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

I've read through the python import system documentation 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.

UPDATE : 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.

UPDATE 2 : 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 python run.py , but I can't run python -m app like you had said without running into an error. The error is as follows if it helps.

"No module name app. main ; 'app' is a package and cannot be directly executed.

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

I think something went wrong in your execution environment. Here are some explanations.

The virtualenv

See the documentation of virtualenv

If you have followed the tutorial:

  • The flask directory is your virtualenv,
  • On posix system, you have a flask/bin subdirectory, or
  • On Windows system, you have a flask\\Scripts subdirectory.

I make the assumption that you are on posix system.

To activate your virtualenv, run:

source flask/bin/activate

Your prompt should change to something like: (flask)$ .

To list the installed libraries use pip :

pip list

Make sure you see Flask. The tutorial encourages you to install a lot of Flask plugins, so there are a lot of Flask-Something…

If Flask is missing, install it:

pip install Flask

Run your app

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

In this file, you have:

from flask import Flask

app = Flask(__name__)
from app import views

From your FlaskTest/ directory, try to run this script like this:

cd FlaskTest/  # if not in this directory
python -m app

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

If app/views.py exist you should have no error.

=> at this point, we have simulated what run.py imports…

Now write run.py in your FlaskTest/ directory:

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

Run it like this:

python run.py

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

This should start your http server…

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM