Relative path in Xlwings Pythonpath - xlwings

Is it possible to reference the python code in the parent folder of the current folder like this:
PYTHONPATH = "../../Python_Code"
It won't work on my computer.
The objective is to avoid hard coded paths.

Indeed, you can set this like so:
PYTHONPATH = ThisWorkbook.Path & "/.."
You might need at least v0.7.0 though.

Related

How do I create a variable named for the folder that is my current working directory?

I want to create a variable that's value is the name of the folder that I am working in. I can use pwd to see the working directory but I cannot figure out if there is a way to save the pwd output as a variable or better yet make a variable with only the current folder not the entire directory.
Here is one method:
#!/usr/bin/env python2.7
import os
current_folder = os.getcwd().split(os.sep)[-1]
print(current_folder)

assigning permanent custom paths to anaconda

This question has been asked, but oddly enough the answers are out of date or not sufficient.
I have installed anaconda, I have setup an environment running py2.7. I launch a win7 cmd prompt and type activate python27 (my custom python environment). I then import sys and then sys.path to see what my python paths are. They all point to variations of E:\Users\myname\Anaconda3\....
I want to add a custom path to this list so that it becomes permanent. Doing sys.path.append is not good enough as its not permanent. I have been reading that adding PYTHONPATH to the environment variables is not done any more, and that I should be adding my custom paths to the PATH system variables.
So could someone advise where and how I can assign my custom paths.
Okay thanks to my instructor I found this to work for me.
open a command prompt (just regular win cmd) and add a path like this where you see the J:\pythonTest you can add your own path this will put it into your path.
SETX PYTHONPATH "%PYTHONPATH%;J:\pythonTest"
One thing I noticed, because I had been trying all sort of ways to solve this problem, my user variables and system variables and a few duplicates of my custom path. I would advise removing them once you run the above command. I suggest removing them because I think I discovered some conflicts and my modules wouldn't run, removing the duplicates seemed to fix this conflict.

Django tutorial path: TEMPLATE_PATH dir on a Mac networked computer

I'm (slowly!) working my way through the Django tutorial, and I've reached the point in Part 2 where I'm supposed to set the template_dir. I'm on a Mac (at work) where my user profile resides on a server, and I can't figure out how to set the path.
The tutorial files are in a folder called "tutorialshell", inside a folder called "Django," which is a first-level file inside my user folder "mattshepherd". That folder is the native folder when I launch the Terminal, for instance: it always starts me inside "mattshepherd".
I've tried
"~/Django/tutorialshell/templates"
and
"home/Django/tutorialshell/templates"
with no luck so far. I imagine there's some trick to doing this, as the files I'm trying to link to are on the network drive in my user folder, not on my local hard drive. Advice?
You want the absolute, not relative path. If you go to ~/Django/tutorialshell/templates in your terminal and then type pwd, it will tell you the full path to that folder. That's the value you should enter for the path.
Also: I assume you're actually talking about TEMPLATE_DIRS? If so, keep in mind that it's a list of paths, so it should look like:
TEMPLATE_DIRS = (
"/path/to/Django/tutorialshell/templates", # don't forget that trailing comma!
)
/Users/mattshepherd/Django/tutorialshell/templates
Please keep in mind that there is a LIST of template directories as mentioned by Jordan. The above location should work. In mac the user home directories are located at /Users/ + yourusername
It might be /home/ if /Users/ does not work.
As others have said you need the absolute path. But i would really suggest you to use relative path. It will make your code much more flexible. There are many ways to do it, this is what i usually do:
import os
ROOT = os.path.abspath(os.path.dirname(__file__))
then for the templates just do:
os.path.join(ROOT, 'templates')
You said you are using a Mac but you can make your code even more flexible by replacing "//" with "/" in the case you are using windows.

eclipse cannot find my templates dir in django project

recently I want to learn django in eclipse, but when I set up eclipse environment , I get a problem...
the django project cannot find my templates folder in eclipse
in the setting.py:
import os
TEMPLATE_DIRS = (
os.path.abspath('templates'),
)
the templates folder:
D:\django_workspace\eagle\eagle\templates
I run 'manage.py shell' and get:
>>> os.path.abspath('templates')
'D:\\django_workspace\\eagle\\eagle\\templates'
but, in eclipse I run the command:
>>>os.path.abspath('templates')
'D:\\Program Files\\eclipse\\templates'
it seems that the os root path is set to be 'D:\Program Files\eclipse' where I install the eclispe
how can I solve this problem ??
thx
The way you're doing it, you're getting the absolute path from a relative path based on the current working directory, so, your code will fail depending on your current directory...
Better would be actually calculating the path based on __file__ from your module (something as os.path.join(os.path.dirname(__file__), 'templates') -- not really sure where your settings file is relative to your templates, so, in your environment it may be a bit different.

What's the common way to layout a Django app with Buildout/djangorecipe?

I have a Django app that I've set up using Buildout laid out like so:
/workspace
/bin
/src
/myproject
settings.py
/myapp
views.py
...
bootstrap.py
buildout.cfg
setup.py
The issue is that I'd like both myproject.settings and myapp on the python path. I need the myproject.settings on the path so djangorecipe can import it. And I'd like myapp on the path so that I don't have to write import myproject.myapp all the time.
For now I've got both /workspace/src and /workspace/src/myproject in the Python path, but this feels like a hack and practically makes me worried if there might be situations where import some_module might have confusing resolution patterns because I have two directories that are parent-child to each other.
So questions are:
Is there an accepted way to lay this out?
Is it actually bad to have a directory and one of its sub-directories in the path?
There is no problem, on import some_module importer will search in each folder specified at sys.path for some_module/__init__.py and some_module.py. Same for import myproject.some_module, it will search for myproject module, then it will try to find in it some_module with same algorithm.
I'm using the same project structure.
If your buildout.cfg includes develop = . and whatever egg your setup.py defines is included as a dependency for your buildout/parts then whatever code path your setup.py defines will be automatically added to sys.path. Just make sure your setup.py includes src as a code directory. One way to do this is with:
setup(name=...
...
packages=find_packages('src'),
package_dir = {'':'src'},
...