pycharm + flask surprising working directory - flask

(Modified to answer the questions in the first comment.)
I am using Flask in PyCharm and getting different results than when I run it from the terminal. In particular, the working directory of the Python program is different under Pycharm. So this code works correctly when running flask from the command line and the print os.getcwd() displays '/Users/Wes/Dropbox/Programming/Python/etpruncnt2'.
However if I run the same code in Pycharm the os.getcwd() displays '/Applications/PyCharm.app/Contents/bin' and the file open fails
with IOError: [Errno 2] No such file or directory: 'data/results.json'.
The root of the project in PyCharm is /Users/Wes/Dropbox/Programming/Python/etpruncnt2. I have also added a screenshot of the project structure in the PyCharm GUI.
I suppose I must have the configuration wrong in PyCharm. I have included a screenshot.
What should I do?
#app.route('/running_count')
def running_count():
print os.getcwd()
with open('data/results.json', 'r') as resultFile:
resultData = json.load(resultFile)
return str(resultData)

You Only Have Shown Script Path
How is project structure ? When you are creating new project you are telling pycharm the project root and may you mixed that part :)

I am going to close this. The immediate problem is that the Working Directory in the config was not filled in.
Most default setups for PyCharm do fill that in, but the setup for flask does not.
I have filed a low-urgency bug report with IntelliJ.

Related

MySQLdb import works from command line but crashes in PyCharm

I am using a Python 2.7 virtualenv with the MySQLdb package installed.
If I run Python from the command line and execute import MySQLdb, this works without error. If I run it from the PyCharm terminal, however, I get an error:
ImportError: libmysqlclient.so.20: cannot open shared object file: No such file or directory
The same pattern occurs if I execute a file test.py containing the line import MySQLdb. It works when executed from the command line and crashed when executed from PyCharm.
I have googled the error and it seems that uninstalling and reinstalling MySQLdb could fix it. But I would like to understand why the error only occurs in PyCharm.
I have made sure that both the command line and the PyCharm terminal use
the same virtual environment (by checking sys.executable)
the same working directory (by checking os.getcwd())
the same path (by checking sys.path)
I have also checked that PYTHONPATH is undefined.
What other difference could there be?
You have to point pycharm to your virtualenv. Go to settings -> project interpreter and give pycharm the path to your python executable. Once there it should work. Note if you have a hybrid WSL/windows setup you will need one virtualenv for WSL and a separate virtualenv for windows/pycharm.

Why does Pycharm still refer to the old directories for my project

Am new to Flask development and am using an IDE like Pycharm for the first time, so please pardon the ignorance.
So while playing around with a project that I am working on to understand Flask, I created a virtual environment by the name venv. So the tree of my project would look something like :
my_project_directory
Project_specific_directories_and_files
requirements.txt
venv
To activate the virtual env, I used to do venv/bin/activate and my project would run as needed.
I came across virtualevnwrapper later and decided to use that. After installing it and setting it up, I moved over to the virtualenvwrapper way of working with virtual envs and completely removed(deleted) the venv directory from my project structure. The new project structure after deleting venv is as :
my_project_directory
Project_specific_directories_and_files
requirements.txt
Everything works fine. The project runs as expected.
However, now after the venv directory was removed, when I open my project in Pycharm, for my import statements like :
from flask import Flask, render_template, redirect, url_for, request, session, flash # , g
from flask_sqlalchemy import SQLAlchem
all the packages that I try importing gets underlined as error in the Pycharm IDE. I opened the Python console, in Pycharm, trying to debug the error and it gives the following error :
Error:Cannot run program "/Users/my_user_name/Desktop/some_parent_directory/my_project_directory/venv/bin/python" (in directory "/Users/my_user_name/Desktop/some_parent_directory/my_project_directory"): error=2, No such file or directory
Now I thought that apparently Pycharm is still using it's cache. So I tried invalidating the cache and restarting Pycharm as explained here, but the problem was still there.
Now I have 2 questions:
Why is Pycharm behaving this way? I removed the venv directory completely etc. Why is it still referencing the old (venv) location?
If it really is not able to resolve the dependencies, how exactly then is my project running all fine? Shouldn't it break the app?
Ok. After some further digging now, I was able to get the errors flagged in Pycharm rectified. I went to Pycharm -> Preferences -> Project: my_project_directory -> Project Interpreter & found that it was 'for some reason' still pointing to the older venv directory. From the drop down of the 'Project Interpreter:' section, I chose the path to my virtualenv created by virtualenvwrapper & saved the changes by Apply->Ok. This removed the error indications from the code in Pycharm. Although solved, the mystery of what/why was this happening is still unsolved for me. – qre0ct Jul 12 at 17:13
Moreover, I also removed the unused/deleted interpreters from the list of project interpreters by going to the 'more' section in the 'Project Interpreter' part and using the '-' at the bottom to remove the ones not needed.

Postgres.App + Django and proper path to libgeos_c.dylib on OSX?

I have succesfully installed Postgres.App. It works just fine if I have this in settings.py:
GEOS_LIBRARY_PATH="/Applications/Postgres.app/Contents/Versions/latest/lib/libgeos_c.dylib"
If I remove the above line and execute manage.py loaddata mydata, I get:
ImportError: Could not find the GEOS library (tried "geos_c", "GEOS"). Try setting GEOS_LIBRARY_PATH in your settings.
The question: is it possible to make things work without GEOS_LIBRARY_PATH set in settings.py? The reason is that in our team not all are using OSX + Postgres.App and the path is not the same for all developers. Django Docs mention usage of LD_LIBRARY_PATH, but having the next line exported in bash did nothing:
LD_LIBRARY_PATH="/Applications/Postgres.app/Contents/Versions/latest/lib/"
I still get the same error. Am I having a wrong syntax or is there another solution so that everyone can have individual path to the library?

Flask PyCharm Syntax error

I've just started developing a new application using Flask and PyCharm as my IDE.
I've encountered a problem which is driving me crazy.
Here is the code. I'll spare use all the import part and so on, I'll show only the routes that cause problems.
#app.route("/")
def home():
return render_template("home.html")
#app.route("/login/")
def login():
return "foo"
The first route works without a problem. However on the first line of the second route, PyCharm gives me an error message '"#" or "def" expected'. This breaks the autoindent and the auto completion features.
But the code runs without a problem.
Can anyone tell me how to fix this?
Thank you
This is because PyCharm is not able to recognize the flask package or version.
Once you add new project to PyCharm -
Go to File menu item
Click on Settings option
This will open-up the pop-up window.
In the left-hand-side link list - click on the project name
Click on the Project Interpreter link
In the right-hand-side pane select Flask and related packages that your project needs and add/install the same.
What worked for me was (I was missing pycharm's python environment dependencies for python)
If you don't already have a requirements file for your project.
Navigate to your project & this on your command line (as I used a venv virtual environment):
(venv)$ pip freeze > requirements.txt
Then Open your pycharm and open the requirements file, wait a second or two and a pop up notification will show up at the top showing you are missing dependencies and will provide you a click here to install.
After that I had no issues.

python import cx_Oracle error in command window

I'm having trouble running some codes that import cx_Oracle in command line, though the same codes work in console. Is there anything else I will need to set up in order to get this to work via command line please?
Saved just one line of code "import cx_Oracle" as test.py.
Run this line in ide (Spyder), iPython Notebook => no issues
run this by opening a command line window from the same folder the .py file is saved in, and run python test.py and encounter the below:
import cx_Oracle
ImportError: DLL load failed: %1 is not a valid Win32 application.
Not sure if there is anything additional I will need to set up to run cx_Oracle via command line? Have tried all the suggestions on setting PATH, ORACLE_HOME, re-installing but could not get this to work. versions that i'm using are
Python: 2.7
cx_Oracle: cx_Oracle-5.1.3-11g.win-amd64-py2.7
instant client: 12.1.0.0
Windows: 7 Enterprise
I also found this kind of problem.
Look at "not a valid Win32 application" this sentence, so I decide to change cx_Oracle to cx_Oracle-5.1.3-11g.win-32-py2.7. Luckly, it does work.