You are not logged in.

#1 2009-04-30 17:36:14

genisis300
Member
From: Uk
Registered: 2008-01-15
Posts: 284

python from foo import bar

hi all,
just working on my new release for batterymon however now i've split the source into several files when i try to run it comes up saying that it can't find the modules.
they are stored under /usr/share/batterymon/ when you install via the PKGBUILD that i'm working on.

does anyone know how i can fix this so that i can do the following
1. run from any location on the command line.
2. run from the working dir of the project without having /usr/share/batterymon in place?

Regards
Matthew


"is adult entertainment killing our children or is killing our children entertaining adults?" Marilyn Manson

Offline

#2 2009-04-30 17:55:13

lswest
Member
From: Munich, Germany
Registered: 2008-06-14
Posts: 456
Website

Re: python from foo import bar

genisis300 wrote:

hi all,
just working on my new release for batterymon however now i've split the source into several files when i try to run it comes up saying that it can't find the modules.
they are stored under /usr/share/batterymon/ when you install via the PKGBUILD that i'm working on.

does anyone know how i can fix this so that i can do the following
1. run from any location on the command line.
2. run from the working dir of the project without having /usr/share/batterymon in place?

Regards
Matthew

For 1: you can place the actual script in /usr/bin, or create a symlink to the script in /usr/bin (after making the actual script executable).

Example:

sudo chmod +x /home/lswest/batterymon
sudo ln -s /home/lswest/batterymon /usr/bin/batterymon

For 2. I'm not sure what exactly what you mean...maybe create symlinks for those modules?


Lswest <- the first letter of my username is a lowercase "L".
"...the Linux philosophy is "laugh in the face of danger". Oops. Wrong one. "Do it yourself". That's it." - Linus Torvalds

Offline

#3 2009-04-30 18:48:37

genisis300
Member
From: Uk
Registered: 2008-01-15
Posts: 284

Re: python from foo import bar

the main script batterymon.py is stored in /usr/bin if i link the other scripts in there do i still need to make them executable?
it does seem a little messy to link all the py files into /usr/bin

from what i can work out python is trying to load the module but because it's not in the python modules path it can't find it. and beacuse the batterymon program can be launched from anywhere the working dir is set to the location that you run it from . eg ~/


"is adult entertainment killing our children or is killing our children entertaining adults?" Marilyn Manson

Offline

#4 2009-04-30 18:54:52

lswest
Member
From: Munich, Germany
Registered: 2008-06-14
Posts: 456
Website

Re: python from foo import bar

You could also place the modules in the python path, but I'd suggest putting it in a folder outside of /usr/bin and seeing if you can specify a path when importing something in Python (I'm not sure you can).

To answer you question, I think they will need to be executable if you expect to run them, but if they will just be imported chances are they don't need to be (you can always just test this out though).

Hope that helps,
Lswest


Lswest <- the first letter of my username is a lowercase "L".
"...the Linux philosophy is "laugh in the face of danger". Oops. Wrong one. "Do it yourself". That's it." - Linus Torvalds

Offline

#5 2009-04-30 18:55:10

genisis300
Member
From: Uk
Registered: 2008-01-15
Posts: 284

Re: python from foo import bar

this is exactly my problem but i'm not 100% sure on using the fix that has been sugested. has anyone come across this before?

http://utcc.utoronto.ca/~cks/space/blog … Workaround


"is adult entertainment killing our children or is killing our children entertaining adults?" Marilyn Manson

Offline

#6 2009-04-30 19:17:57

lswest
Member
From: Munich, Germany
Registered: 2008-06-14
Posts: 456
Website

Re: python from foo import bar

That should work fine, it's like putting the program in a folder and running a bash script with "python /path/to/batterymon "$@"" where "$@" are all the command-line arguments being passed to the script.  So you could move the batterymon.py to the same folder as the modules, and create a new script in /usr/bin with python /usr/share/batterymon/batterymon.py "$@" which will effectively run the program like batterymon.py <arguments>.


Lswest <- the first letter of my username is a lowercase "L".
"...the Linux philosophy is "laugh in the face of danger". Oops. Wrong one. "Do it yourself". That's it." - Linus Torvalds

Offline

#7 2009-04-30 19:25:27

Xyne
Administrator/PM
Registered: 2008-08-03
Posts: 6,963
Website

Re: python from foo import bar

Install your modules to "/usr/lib/python2.6/site-packages/batterymon/". That seems to be how other packages do it.


My Arch Linux StuffForum EtiquetteCommunity Ethos - Arch is not for everyone

Offline

#8 2009-04-30 20:39:44

Yannick_LM
Member
Registered: 2008-12-22
Posts: 142

Re: python from foo import bar

I would recommend using distutils and a setup.py file:

http://docs.python.org/distutils/introduction.html

Offline

#9 2009-04-30 20:53:02

Xyne
Administrator/PM
Registered: 2008-08-03
Posts: 6,963
Website

Re: python from foo import bar

Yannick_LM wrote:

I would recommend using distutils and a setup.py file:

http://docs.python.org/distutils/introduction.html

Forget my previous suggestion. This one is much better.


My Arch Linux StuffForum EtiquetteCommunity Ethos - Arch is not for everyone

Offline

#10 2009-04-30 21:08:25

robmaloy
Member
From: Germany
Registered: 2008-05-14
Posts: 263

Re: python from foo import bar

iirc you could also use sys.path.append()


☃ Snowman ☃

Offline

Board footer

Powered by FluxBB