Is there a more elegant way to change the working directory of Python
to the directory of the currently executing script, and add a folder
called 'Shared' to the Python search path?
This is what I have. It seems like it could be shorter, somehow.
# Switch Python to the current directory
import os, sys
pathname, scriptname = os.path.split(s ys.argv[0])
pathname = os.path.abspath (pathname)
os.chdir(pathna me)
s = os.path.realpat h('..')
s = os.path.join(s, 'Shared')
sys.path.append (s)
Thanks for any enhancements you can suggest! 6 5010
Hello,
Seems you found os.chdir(), but you missed os.getcwd()
;)
M.E.Farmer
Ignore my last post to much eggnog I guess. I must have parsed it to
quickly.
M.E.Farmer
Kamilche wrote: Is there a more elegant way to change the working directory of Python
That depends on how you define "elegant", I guess.
to the directory of the currently executing script, and add a folder called 'Shared' to the Python search path?
This is what I have. It seems like it could be shorter, somehow.
It could be shorter if you were willing to combine several
function calls on the same line, but I get the impression
you wouldn't consider that more elegant...
# Switch Python to the current directory import os, sys pathname, scriptname = os.path.split(s ys.argv[0]) pathname = os.path.abspath (pathname) os.chdir(pathna me) s = os.path.realpat h('..') s = os.path.join(s, 'Shared') sys.path.append (s)
Thanks for any enhancements you can suggest!
Other than using os.pardir instead of '..', and possibly adding
an "os.path.abspat h()" call to the last bit (or does realpath
already do that? It's unclear from the docs), I can't see
anything fundamental I'd do differently... except package these
functions up as nice clean subroutines, possibly in a library
package, that I could then use in code that would magically
become "elegant" (IMHO) by avoiding the repetition of all
that non-elegant stuff above...
-Peter
On Mon, 27 Dec 2004 11:53:57 -0500, Peter Hansen <pe***@engcorp. com> wrote: Kamilche wrote: Is there a more elegant way to change the working directory of Python
That depends on how you define "elegant", I guess.
to the directory of the currently executing script, and add a folder called 'Shared' to the Python search path?
This is what I have. It seems like it could be shorter, somehow.
It could be shorter if you were willing to combine several function calls on the same line, but I get the impression you wouldn't consider that more elegant...
# Switch Python to the current directory import os, sys pathname, scriptname = os.path.split(s ys.argv[0]) pathname = os.path.abspath (pathname) os.chdir(pathna me) s = os.path.realpat h('..') s = os.path.join(s, 'Shared') sys.path.append (s)
Thanks for any enhancements you can suggest!
How about ?
import os, sys
pathname, scriptname = os.path.split(s ys.argv[0])
pathname = os.path.abspath (pathname)
os.chdir(pathna me) # only if you really need to cwd
parent = os.path.dirname (pathname)
parent = os.path.join(pa rent, 'Shared')
if parent not in sys.path:
sys.path.append (parent)
Or even :
pathname, scriptname = os.path.split(_ _file__)
# pathname should be an absolute path
> For "library" modules there are already other and more elegant means. -Peter
Well, I want to make a script execute without error regardless of where
the current working directory is, and without having to make the user
modify their PYTHONPATH variable or install something in site-packages.
Is there a way to let the user just 'drag the folder' anywhere on their
hard drive, and have it work, without having to modify any special
system variables?
Kamilche wrote: For "library" modules there are already other and more elegant means. Well, I want to make a script execute without error regardless of where the current working directory is, and without having to make the user modify their PYTHONPATH variable or install something in site-packages.
If you are talking about just "a script", then this of course
works out of the box without doing anything special. You just
run the script and it works.
If, on the other hand, the script requires access to certain
shared *other* modules, then no, if you don't tell the script
where to find those other modules, and they are not in the same
folder as the script itself, then you can't do this. You do
have to tell the Python where to find things... I'm unclear
what your situation is, that you have shared modules that you
want to use in various other scripts, and yet you have some
strange restriction preventing you from telling Python where
those are to be found except with custom code written in each
script that will use the modules.
Even if that really describes your (very bizarre, IMHO) situation,
the least you could do is write the aforementioned code in a
nice subroutine that you just cut and paste into each module,
then call "elegantly" . It's code reuse, even if it's the most
rudimentary sort of reuse.
Is there a way to let the user just 'drag the folder' anywhere on their hard drive, and have it work, without having to modify any special system variables?
Not sure exactly what you are looking for. I certain have folders
of Python scripts that I can just drag here or there and have them
work. When they need to use common library files, however, I take
a moment to set that up ahead of time, using .pth files if I can't
or won't install them in site-packages.
-Peter This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Steve |
last post by:
Hey, well I'm really pleased with myself for writing a little script today
that changes variables according to a radio button. The thing is, I have
this feeling part of my script could be a lot more elegant and compact than
it is:
if ($radiobutton=="radio1") {$item1 =$item1 * 54 ;}; //each of these blocks
is a range each block assigns different prices to items
if ($radiobutton=="radio1") {$item2 =$item2 * 56 ;};
if...
|
by: Kamilche |
last post by:
'''
Is there a more elegant way of doing this?
I would like to have the arguments to pack
automatically taken from lst.
'''
def pack(self):
lst =
return struct.pack('<IIIiiiBBBHHH', \
self.id, self.parent, self.number, \
|
by: Fresh Air Rider |
last post by:
Hello
Could anyone please explain how I can pass more than one
arguement/parameter value to a function using <asp:linkbutton> or is
this a major shortfall of the language ?
Consider the following code fragments in which I want to list through
all files on a directory with a link to download each file by passing
a filename and whether or not to force the download dialog box to
appear.
|
by: Edward W. |
last post by:
hello, I have this function below which is simple and easy to understand
private function ListHeight (byval UserScreenHeight as int) as int
if UserScreenHeight < 1024
return 30
else
return 50
end if
end function
|
by: Iain King |
last post by:
When I loop over one list I use:
for item in items:
print item
but often I want to loop through two lists at once, and I've been doing
this like I would in any other language - creating an index counter and
incrementing it.
For example, (completely arbitrary), I have two strings of the same
length, and I want to return a string which, at each character
| |
by: sherifffruitfly |
last post by:
Hi all,
This is how I'm currently getting Friday of last week. It strikes me as
cumbersome. Is there a slicker/more elegant way?
Thanks for any ideas,
cdj
|
by: Bjoern Hoehrmann |
last post by:
Hi,
For a free software project, I had to write a routine that, given a
Unicode scalar value U+0000 - U+10FFFF, returns an integer that holds
the UTF-8 encoded form of it, for example, U+00F6 becomes 0x0000C3B6.
I came up with the following. I am looking for a more elegant solution,
that is, roughly, faster, shorter, more readable, ... while producing
the same ouput for the cited range.
unsigned int
|
by: Anonymous |
last post by:
I want to be able to restrict the set of classes for which a template
class can be instantiated (i.e enforce that all instantiation MUST be
for classes taht derive from a base type BaseType).
This occured to me immediately, but use of a dummy variable is not
elegant - are there other (more elegant) ways of doing this?
template <class DerivedType>
class MyClass
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |