I set up a new virtualenv for my app, to clean up the installed packages. I have a config.py
file in the app's instance folder. There are two configs, one for dev in the 'instance' folder and one for production in the root folder. The app worked in the old env, but now I get an error that it couldn't find the config file. Why am I getting this error and how do I fix it?
Traceback (most recent call last):
File "application.py", line 12, in <module>
application.config.from_pyfile('config.py')
File "/Users/pavsidhu/Envs/test/lib/python2.7/site-packages/flask/config.py", line 128, in from_pyfile
with open(filename) as config_file:
IOError: [Errno 2] Unable to load configuration file (No such file or directory): '/Users/pavsidhu/Envs/test/var/application-instance/config.py'
My project structure looks like:
/Users/pavsidhu/Envs/test/
lib/python-3.4/site-packages/
bin/
application.py
static/
templates/
instance/
config.py
The instance folder is a sub-directory of the repository root and contains a configuration file specifically for this instance of the application. We don't want to commit it into version control. config.py requirements.txt run.py instance/ config.py yourapp/ __init__.py models.py views.py templates/ static/
An application configuration file contains settings that are specific to an app. This file includes configuration settings that the common language runtime reads (such as assembly binding policy, remoting objects, and so on), and settings that the app can read.
Flask thinks your app is installed in the env because you put it in the same base directory as the env. This behavior is described in detail in the docs.
Installed module or package:
$PREFIX/lib/python2.X/site-packages/myapp $PREFIX/var/myapp-instance
$PREFIX is the prefix of your Python installation.
Flask expects the instance folder to be at var/myapp-instance
when installed this way. (The structure isn't exactly the same, but it's similar enough to fool Flask's detection function.)
You should not mix your project structure with the virtualenv structure. Separate the two:
my_env/
lib/
bin/
my_project/
my_app/
__init__.py
static/
templates/
instance/
See the comment discussion moved to chat for further details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With