How to use own python modules without rewriting all paths in Django - python

I am making a website with django that uses my python codes. my directory is like this:
\mysite
\myApp
\myCodes
\common
\deploy
\src
misc.py
def Args():
# commands
\mySite
my codes in /myCodes/src have a lines at the beginning like:
import sys
sys.path.append('../deploy')
sys.path.append('../common')
some codes in /common and /deploy also have the same lines and they work fine but when I use my codes in from the view.py they can't find each other, I added \myCodes as an app in settings.py and when I manually change the import dir (import myCodes.common.myFunction) it is fine but it seams to be unnecessary to manually change all of the imports, is there away I can execute the codes as they are?
P.s: I also use directory as input and open and write files in my functions that don't work (the are all in \myCodes directory)

I used os.path.dirname(__file__)+'/rest of the dir/' to give full path and it worked

Related

Errno 2 No such file or directory error while importing a python script from a sub-folder

I have a python script (db.py) which has a class(Chat) which refers to a json file(cred.json) saved in the same folder(db). This script runs perfectly fine without any error.
When I try to import the class Chat in a python script(wa.py) which is saved in one folder above folder db I get an error saying "[Errno 2] No such file or directory".
My folder structure looks like below
- wa.py
- db
- db.py
- cred.json
Piece of code in db.py where I am referring to the json file
cred_filename = 'creds.json'
with open(cred_filename, 'r') as c:
data = json.load(c)
wa.py looks like this
from db.db import Chat
I tried to find answer on google but couldn't find a relevant explanation for my case. I know there is something fundamentally wrong here but I am not able to figure it out.
Thanks in advance.
Essentially you're trying to open creds.json which is relative to your current working directory (unless changed during the runtime, one you have been in when calling the script) which does not need to be and apparently when this happens is not the one in which db.py resides.
This can only work and worked, as long as you were already in db/ when importing / running db.py.
If you always wanted to get creds.json in the directory where db.py resides, you could say for instance:
from pathlib import Path
...
cred_filename = Path(__file__).with_name("creds.json")
That takes path of the file this module is imported from and replaces the filename part with "creds.json".

Running python script with new directories

I have recently begun working on a new computer. All my python files and my data are in the dropbox folder, so having access to the data is not a problem. However, the "user" name on the file has changed. Thus, none of my os.chdir() operations work. Obviously, I can modify all of my scripts using a find and replace, but that won't help if I try using my old computer.
Currently, all the directories called look something like this:
"C:\Users\Old_Username\Dropbox\Path"
and the files I want to access on the new computer look like:
"C:\Users\New_Username\Dropbox\Path"
Is there some sort of try/except I can build into my script so it goes through the various path-name options if the first attempt doesn't work?
Thanks!
Any solution will involve editing your code; so if you are going to edit it anyway - its best to make it generic enough so it works on all platforms.
In the answer to How can I get the Dropbox folder location programmatically in Python? there is a code snippet that you can use if this problem is limited to dropbox.
For a more generic solution, you can use environment variables to figure out the home directory of a user.
On Windows the home directory is location is stored in %UserProfile%, on Linux and OSX it is in $HOME. Luckily Python will take care of all this for you with os.path.expanduser:
import os
home_dir = os.path.expanduser('~')
Using home_dir will ensure that the same path is resolved on all systems.
Thought the file sq.py with these codes(your olds):
C:/Users/Old_Username/Dropbox/Path
for x in range:
#something
def Something():
#something...
C:/Users/Old_Username/Dropbox/Path
Then a new .py file run these codes:
with open("sq.py","r") as f:
for x in f.readlines():
y=x
if re.findall("C:/Users/Old_Username/Dropbox/Path",x) == ['C:/Users/Old_Username/Dropbox/Path']:
x="C:/Users/New_Username/Dropbox/Path"
y=y.replace(y,x)
print (y)
Output is:
C:/Users/New_Username/Dropbox/Path
for x in range:
#something
def Something():
#something...
C:/Users/New_Username/Dropbox/Path
Hope its your solution at least can give you some idea dealing with your problem.
Knowing that eventually I will move or rename my projects or scripts, I always use this code right at the beginning:
import os, inspect
this_dir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
this_script = inspect.stack()[0][1]
this_script_name = this_script.split('/')[-1]
If you call your script not with the full but a relative path, then this_script will also not contain a full path. this_dir however will always be the full path to the directory.

Where should I place a config.txt in my python project

The project folder hierarchy looks like
ProjectName
->src
->Project.sikuli
->myFile.py
->config.txt
Now, I have all the settings variables being stores in my config.txt and I'm using ConfigParser to fetch the values from it. The reason why I'm using this config file here is that, when this sikuli script is moved to another machine for running I can just change the values in it (like paths, username, password) rather than editing the main python script 'myFile.py'.
But the issue I'm encountering now is that I don't want the config file to be placed some where outside the project so that in my script when I try to fetch the values from it, I don't have to mention the absolute path again in the myFile.txt like:
configParser = ConfigParser.RawConfigParser()
configfilePath = r'D:\MyWorkspace\ProjectName\src\Project.sikuli\config.txt'
Instead I want to have the relative path here so that while migrating the project from machine to machine I don't have to do any manipulations in the main script 'myFile.py'
So what I'm trying to achieve is like:
I should be able to refer the config.txt file by giving it's relative path:
configfilePath = r'D:\MyWorkspace\ProjectName\src\Project.sikuli\config.txt'
If it's going to be kept in the same folder as myFile.py, then in that Python script you could use something like this:
configfilePath = os.path.join(os.path.dirname(__file__), 'config.txt')
The best way to do this is to put the file inside your .sikuli bundle, just as you have done in your example, and then get the path to your file like this:
configFilePath = os.path.join(getBundlePath(), 'config.txt')
I know you can fetch variables and value from a python file by importing it...
import config
I would put it in the root repertory of the project
First, get the path of the currently executed python script:
myPath = os.path.abspath(os.path.dirname(sys.argv[0]))
and then do a join of myPath and 'config.txt'
configfilePath = os.path.join(myPath, 'config.txt')

How to find a file in the subfolder?

I have a script that uses icons on buttons. When I leave icon.gif in the folder with the script the icons can be found without an error. But I want to keep these icons in myicons folder and use them from that folder. So the structure would be like this: myscript_folder/myicons/icon_1.gif
I have a variable that keeps the path to icons, but as you've guessed the variable has only 'icon.gif' in it.
How do I add a path to "myicons" folder to this variable which is os independent?
You can use the join function from the os.path module. This module comes in different versions for different operating systems, each using the respective file separators, etc.
import os.path
os.path.join("myicons", "icon_1.gif")
Try this, which is very same as tobias_k's reply.
import os
base_dir = r"path/to/your/myscript_folder"
print os.path.join(base_dir, "myicons", "icon_1.gif")

Running bash scripts within newly created folders based on file names

I'm not sure even where to start.
I have a list of output files from a program, lets call them foo. They are numbered outputs like foo_1.out
I'd like to make a directory for each file, move the file to its directory, run a bash script within that directory, take the output from each script, copy it to the root directory as a concatenated single file.
I understand that this is not a forum for "hey, do my work for me", I'm honestly trying to learn. Any suggestions on where to look are sincerely appreciated!
Thanks!
You should probably look up the documentation for the python modules os - specifically os.path and a couple of others - and subprocess which can be found here and here respectively.
Without wanting to do it all for you as you stated - you'll be wanting to do something like:
for f in filelist:
[pth, ext] = os.path.splitext(f)
os.mkdir(pth)
out = subprocess.Popen(SCRIPTNAME, stdout=...)
# and so on...
To get a list of all files in a directory or make folders, check out the os module. Specifically, try os.listdir and os.mkdir
To copy files, you could either manually open each file, copy the contents to a string, and rewrite it to a different file. Alternatively, look at the shutil module
To run bash scripts, use the subprocess library.
All three of those should be a part of python's standard library.

Categories