# Debugging Common Problems

## Python and Virtual Environments

Problems with Python and virtual environments.

### How do I run my virtual environment?

Virtual environments don't really run; rather, you activate them so that you can use them. If you set the virtual environment as your project interpreter in PyCharm, that will automatically activate it for the purpose of running your programs from PyCharm.

If you are in a terminal or command prompt, you need to activate it for that terminal. This needs to be done for each terminal session; closing the terminal forgets the activation. All activation does is set some environment variables so that your terminal look for python, pip, and related programs in the virtual environment instead of in global system locations.

To activate on OS X or Linux:

source /path/to/venv/bin/activate


To activate in PowerShell or Command Prompt on Windows:

C:\Path\to\venv\Scripts\activate


### pip install says that requirements already met, but the app doesn't work

If you run pip install Flask (or some other package), and it says the requirements are already met, this means that there is already a version of the package installed either in your virtual environment or in the global Python. pip can get a little confused sometimes about where the package is installed.

pip list will list installed packages. If you start a new terminal where you have not activated your virtual environment and run pip list (or pip3 list on some OS X and Linux installations), it will list the globally-installed packages. If Flask and friends appear there, use pip uninstall to remove them.

On a clean Windows or Mac install of Python, the only things that should be in the pip install list of your global Python installation are pip and setuptools.

Note

If you create a Flask project in PyCharm using your global Python as the interpreter, it will automatically install Flask and friends in the global interpreter. Uninstall them with pip uninstall and create your Flask project using the virtual environment interpreter. You can change the interpreter for an existing project by going to ‘File’ → ‘Settings’ and picking ‘Project project’ / ‘Project interpreter’.

### My application works, but manage.py cannot find flask_script

Symptom: You can run your application directly from PyCharm, but when you run manage.py it reports that flask_script cannot be found.

This is caused by the ‘Flask-Script’ package not being installed.

The most common cause of this problem, if you have already followed the instructions for creating a virtual environment and installing packages, is that PyCharm is set up to use your global Python instead of the virtual environment. When it does this, it automatically installed Flask but does not know that it needs to install Flask-Script.

The fix:

1. Go to PyCharm's Settings, and go to the Project Interpreter option. Set it to use your virtual environment as the project interpreter. You may need to add the virtual environment.
2. Go to each of your run configurations (under RunEdit Configurations…) and change them to use the virtual environment as their interpreter.

Once you are using the virtual environment as the interpreter, if it still cannot find flask_script, then you need to install it. You can do this from within PyCharm by going back to the Project Interpeter option, clicking the little ‘+’ button at the bottom of the package list, and searching for ‘Flask-Script’.

### print doesn't show anything

Flask hooks in to standard output, so an ordinary print statement does not work. There are two ways around this:

• Print to standard error:

print("My message to print", file=sys.stderr)


You will need to import sys at the top of your file.

• Write to the application's logger:

app.logger.info("My message to print")


### I see a 500 Internal Server Error with no details

By default, Flask doesn't provide much info on coding errors, because such information could disclose security-sensitive information about your server's configuration.

However, it provides a debug mode that gives far more information. To activate debug mode, make sure you are running your application with manage.py (instead of running your application file directly), and pass the --debug option to the runserver command. This means that your Script Parameters in the PyCharm Run Configuration should be:

runserver --debug --reload


If you are running from the command line:

python3 manage.py runserver --debug --reload


### My Python/Flask app cannot find data files

If you are running your Flask app, and it cannot find data files (e.g. the glob.glob('data/*.yaml') line from A1 doesn't find any files), there are two things to check:

• Are the data files where you expect them? For example, do you have data/muchado.yaml and not data/shakespeare/muchado.yaml?
• More likely: check the working directory of your run configuration. When you run a Python script, by default PyCharm seems to run it from an arbitrary and useless dirctory. Edit your run configuration and change the Working Directory to your project directory.

## Git and Packaging

### manage.py package complains about untracked files

If the manage.py package command reports that there are untracked files under /.idea, etc., the most likely cause is that you do not have a properly configured .gitignore file.

1. Open gitignore.txt
2. Save its contents in a file in your project called .gitignore
3. Add this file to Git and commit it

### manage.py package says there are untracked changes but PyCharm disagrees

On Windows, if manage.py package says there are changes in all your files, but PyCharm says that everything is up to date, the most likely cause is an end-of-line problem. This is commonly caused by having a Cygwin installation that includes git on your PATH, where manage.py will pick it up.

To check if this is the problem, open PowerShell and run:

Get-Command git


It will print out either the first git it finds, or a list of git commands, depending on your PowerShell version. The first one in the list is the one it will be using by default. If it is in Cygwin (e.g. C:\CYGWIN\bin\git.exe), then you are experiencing this problem.

To fix it, edit your environment variables (open Stat Menu, search for Environment, and select Edit System Environment Variables). Go to the PATH variable; there will be a PATH in both your User and System sections. The System one is usually the one that has the problem.

Make sure that C:\Program Files\Git\cmd (or equivalent) appears before C:\CYGWIN\bin. You may also want to consider removing the Cygwin bin directory from your PATH entirely, as having Unix programs in PATH may confuse Windows programs.

### manage.py package ‘cannot find the file specified’

On Windows, you may see the following error when you run manage.py package:

Traceback (most recent call last):
checking repository status
File "C:/Users/YOURNAME/PycharmProjects/A0/manage.py", line 93, in <module>
manager.run()
File "C:\Users\YOURNAME\CS3320env\lib\site-packages\flask_script\__init__.py", line 412, in run
result = self.handle(sys.argv[0], sys.argv[1:])
File "C:\Users\YOURNAME\CS3320env\lib\site-packages\flask_script\__init__.py", line 383, in handle
res = handle(*args, **config)
File "C:\Users\YOURNAME\CS3320env\lib\site-packages\flask_script\commands.py", line 216, in __call__
return self.run(*args, **kwargs)
File "C:/Users/YOURNAME/PycharmProjects/A0/manage.py", line 56, in package
proc = subprocess.Popen(['git', 'status', '--porcelain'], stdout=subprocess.PIPE)
File "C:\Python34\lib\subprocess.py", line 859, in __init__
restore_signals, start_new_session)
File "C:\Python34\lib\subprocess.py", line 1114, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified

Process finished with exit code 1


The most common cause for this error is the git executable not being in your PATH. If you re-run the Git installer and select the second option, to make it available from the command prompt (not the first option, where Git is only used from Git Bash), that will fix it.

Alternatively, you can edit your environment variables (go to the System Properties control panel, or search for ‘environment’ in Windows 10), edit PATH, and add C:\Program Files\Git\bin (or C:\Program Files (x86)\Git\bin).

You'll need to restart PyCharm and possibly log out of your computer after making the change by either of these means.