Django how to read a file in the app subdirectories folder? - python

I am using Django to build a Web application, and there is python module inside the app folder. This python module needs to interact with yaml in the subdirectories "input" and "output".
map_local_path = "./input/map.yaml"
I just tried to read this path, it will work at my local spyder ide, but it wont work when it comes to Django.
I want to call my own python module api in views.py, and what I am doing is to:
openFile = open("./input/map.yaml", "r")
and I cannot make it work at Django.
How can I fix it? T_T Thanks!
IOError: [Errno 2] No such file or directory: './input/map.yaml'

Related

Python: Changement of path when executing

I am trying to make a desktop app on windows, with every files in a directory called: FINALS.
There are 2 python files (main.py and temp.py) used for logic, backend and these 2 files are using 2 other files .json to save data and use it (data.json and subjects.json)
To be more flexible, instead of copying the full path of the .json files in the python files, I used for exemple:
with open(f"{self.path}\\subjects.json") as subjects_file:
subjects = json.load(subjects_file)
with self.path beeing:
self.path = os.getcwd()
So everything is working fine since a execute code from the python file.
But I created a gui.py to get an interface with tkinter, using main.py and temp.py for the logic which are themselves using data.json and subjects.json for data management.
But when I execute the gui.py, I get quickly an error:
with open(f"{self.path}\\subjects.json") as subjects_file:
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Program Files\\JetBrains\\PyCharm Community Edition 2021.3\\jbr\\bin\\subjects.json'
Apparently Pycharm is changing the path when executing, but only when the GUI is executing ?
Hope it was clear and you will be able to help me
EDIT: Apparently only PyCharm does that because I tried executing the GUI from VSCode and it worked...

Don't understand the why configparser can't find the config file

I'm using cofigparser in my python project. Directory structure is as following:
root/dir/data.py
root/dir/config.ini
data.py code:
config_parser = configparser.RawConfigParser()
config_file_path = "data/config.ini"
config_parser.read_file(open(config_file_path))
This works from local machine. But when Im trying to build it from Jenkins, output is:
> config_parser.read_file(open(config_file_path)) 19:09:04
> FileNotFoundError: [Errno 2] No such file or directory:
> 'data/config.ini'
After many attempts to resolve it I find out, that from Jenkins it works only if I include root to the path, so as I change it as following:
config_file_path = "root/data/config.ini"
It starts working on Jenkins. But - I have above error message on local machine now. So now Im in the situation, that if I can run my code from local machine, I have to change that path, and don`t forget to change it back, when Im commiting changes to git. Has anybody any idea, why is that? And how to write the path which will work on both?
Note1:
All the imports Im using in project are without root directory included, so for example:
from dir.dir2 import foo
If I include root dir, it never works from Jenkins.
Note2:
Both machines running Windows.

Call a file in another folder in Eclipse for Python project

I have a small enough Python project in Eclipse Neon and keep getting the same error and can't find proper documentation on how to solve. In my main I need to call a file that is located in another folder. The error I receive is IOError: [Errno 2] No such file or directory:
I have an empty init.py file in the folder (XML_TXT) that I'm trying to use.
It looks like Groovy is importing okay, or else you would get an ImportError. An IOError indicates that it can't find "test.txt". Does that file exist?
It will work if the file path is relative to where you are running the script from. So for example if test.txt is in a folder
Groovy("folder_name/test.txt")
You can also go up in the directory structure if you need to, for example
Groovy("../folder_name/test.txt")
Or, if you want to be able to run the file from anywhere, you can have python work out the absolute path of the file for you.
import os
filename = os.path.join(os.path.dirname(__file__), 'folder_name/test.txt')
u = Groovy(filename)

IOError: [Errno 2] No such file or directory: 'users.txt'

I am getting the above error when I use a webserver to run my code, however locally in the Terminal this works fine. I believe this must be to do with the path to the file working locally but not remotely. I have seen the solution on stackoverflow is to add the filepath like '/user/xxx/library/' etc, however is there a solution that allows this to be system agnostic? As in if I copy this directory to another location/server it will still work?
You can import os, it's built it to Python. You can get teh absolute path of the .py file this way:
import os
ROOT = lambda base : os.path.join(os.path.dirname(__file__), base).replace('\\','/')
Now you can simply do the following:
ROOT('users.txt')
It should return the absolute path.

Google App Engine deployment: missing file

I have a problem with google app engine. It used to work but now I can't figure out whats wrong...
python: can't open file 'google_appengine/dev_appserver.py': [Errno 2] No such file or directory
apologize for the delay in getting back to this.
1) it happens when command is executed outside google_appengine directory e.g.
abid#abid-webdev:~/Documents/GAE_projects/helloworld$ python google_appengine/dev_appserver.py helloworld/
python: can't open file 'google_appengine/dev_appserver.py': [Errno 2] No such file or directory
2) now when i run from the directory where i have
"google_appengine folder" and project, it works grand :)
abid#abid-webdev:~/Documents/GAE_projects$ ls
google_appengine helloworld
abid#abid-webdev:~/Documents/GAE_projects$ python google_appengine/dev_appserver.py helloworld/
Allow dev_appserver to check for updates on startup? (Y/n): y
dev_appserver will check for updates on startup. To change this setting, edit /home/abid/.appcfg_nag
3) another thing i noticed, google docs says->[https://developers.google.com/appengine/docs/python/gettingstartedpython27/helloworld][1] to use ->
google_appengine/dev_appserver.py helloworld/ this command -
whereas i used
python google_appengine/dev_appserver.py helloworld/ as suggested on udacity forums->
http://forums.udacity.com/questions/6003945/with-opensuse-121-the-python-app-refuses-to-work
thanks all for your help. cheers

Categories