Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #82681 > unrolled thread

Changing script's search path at install time

Started byMitko Haralanov <m.haralanov@gmail.com>
First post2014-12-19 14:51 -0800
Last post2014-12-20 21:07 +1100
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Changing script's search path at install time Mitko Haralanov <m.haralanov@gmail.com> - 2014-12-19 14:51 -0800
    Re: Changing script's search path at install time sohcahtoa82@gmail.com - 2014-12-19 18:18 -0800
    Re: Changing script's search path at install time Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-12-20 21:07 +1100

#82681 — Changing script's search path at install time

FromMitko Haralanov <m.haralanov@gmail.com>
Date2014-12-19 14:51 -0800
SubjectChanging script's search path at install time
Message-ID<mailman.17062.1419040705.18130.python-list@python.org>

[Multipart message — attachments visible in raw view] — view raw

Hi all,

I have a question regarding installation of Python scripts and modules
using distutils that I can't find an answer to by searching through Google
and the Python website. Hopefully, someone on this list might have ideas?

I am writing a Python app, which I would eventually like to install using
disutils. The app has the current tree strucutre:

<root>
  |----- myapp.py
  |----- modules/
               |------  lib/
                         |------  __init__.py
                         |------  mymodule1.py
                         |------  mymdule2.py
               |------- cmds/
                         |------  __init__.py
                         |------  cmds1.py

Naturally, in order for my app to properly import the modules, currently it
uses the following code:

    import modules.lib.mymodule1
    import modules.lib.mymodule2
    import modules.cmds.cmds1

However, when the app gets installed, I would like to install the modules
to /usr/lib64/pythonX.Y/site-packages/myapp. I know that I can do this by
using the "package_dir" argument to the "setup()" function in
distutils.core.

To make development easier I would like to be able to run the myapp.py
script from the development directory, which means that the import
statements have to remain as they are. The issue is that when the script is
installed, the import statements will not work anymore since the directory
name has been changed from "modules" to "myapp".

My problem is that I can't figure out how to modify the myapp.py script to
switch from "modules.lib.mymodule1" to "myapp.lib.module1" at install time.
Does anyone have any useful hints?

Thank you,
- Mitko

[toc] | [next] | [standalone]


#82682

Fromsohcahtoa82@gmail.com
Date2014-12-19 18:18 -0800
Message-ID<6d17cca5-a62b-4f20-aec6-5722305cd2d0@googlegroups.com>
In reply to#82681
On Friday, December 19, 2014 6:00:15 PM UTC-8, Mitko Haralanov wrote:
> Hi all,
> 
> 
> I have a question regarding installation of Python scripts and modules using distutils that I can't find an answer to by searching through Google and the Python website. Hopefully, someone on this list might have ideas?
> 
> 
> I am writing a Python app, which I would eventually like to install using disutils. The app has the current tree strucutre:
> 
> 
> <root>
>   |----- myapp.py
>   |----- modules/
>                |------  lib/
>                          |------  __init__.py
>                          |------  mymodule1.py
>                          |------  mymdule2.py
>                |------- cmds/
>                          |------  __init__.py
>                          |------  cmds1.py
> 
> 
> Naturally, in order for my app to properly import the modules, currently it uses the following code:
> 
> 
>     import modules.lib.mymodule1
>     import modules.lib.mymodule2
>     import modules.cmds.cmds1
> 
> 
> However, when the app gets installed, I would like to install the modules to /usr/lib64/pythonX.Y/site-packages/myapp. I know that I can do this by using the "package_dir" argument to the "setup()" function in distutils.core.
> 
> 
> To make development easier I would like to be able to run the myapp.py script from the development directory, which means that the import statements have to remain as they are. The issue is that when the script is installed, the import statements will not work anymore since the directory name has been changed from "modules" to "myapp".
> 
> 
> My problem is that I can't figure out how to modify the myapp.py script to switch from "modules.lib.mymodule1" to "myapp.lib.module1" at install time. Does anyone have any useful hints?
> 
> 
> Thank you,
> - Mitko

Don't call that subdirectory "modules".  Call it "myappmodules" or something like that.  The modules would then be installed in /usr/lib64/pythonX.y/site-packages/myappmodules

I'm sure there's a work-around that would let you refer to them by different names based on whether or not the script is being run from your development directory, but it would probably be a Bad Idea and could easily break on some peoples' systems.

Alternatively, you could decide if you really need those modules to be installed as stand-alone modules in the site-packages directory.  Are users going to be importing those modules into their own scripts?

[toc] | [prev] | [next] | [standalone]


#82688

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2014-12-20 21:07 +1100
Message-ID<54954a71$0$12992$c3e8da3$5496439d@news.astraweb.com>
In reply to#82681
Mitko Haralanov wrote:

> Hi all,
> 
> I have a question regarding installation of Python scripts and modules
> using distutils that I can't find an answer to by searching through Google
> and the Python website. Hopefully, someone on this list might have ideas?
> 
> I am writing a Python app, which I would eventually like to install using
> disutils. The app has the current tree strucutre:
> 
> <root>
>   |----- myapp.py
>   |----- modules/
>                |------  lib/
>                          |------  __init__.py
>                          |------  mymodule1.py
>                          |------  mymdule2.py
>                |------- cmds/
>                          |------  __init__.py
>                          |------  cmds1.py


Have you considered making the whole app a single package, and getting rid
of the unnecessary (so it seems to me) "modules" directory?

<root>
  +-- myapp/
      +-- __init__.py
      +-- __main__.py
      +-- lib/
          +-- contents of lib
      +-- cmds/
          +-- contents of cmds

The __main__.py is run when you do:

python -m myapp


without needing to know the exact path to it. (So long as it can be found on
the Python path.) That may simplify your development.

If you still insist on having a file myapp.py which appears elsewhere, say
in your home directory, it can include a little hack at the start:

# Untested, and probably not quite enough to work correctly.
import sys
if '.' in sys.path:
    sys.path.remove('.')
import myapp


which is a way of ensuring that the installed myapp *package* shadows the
currently-running myapp.py *module*.

(This, I hasten to add, is a nasty dirty hack. A less nasty way to do this
is to just make the local myapp.py a symlink or hard link to the real
myapp/__main__.py. Better still is not to use the same name for the package
and the script.)




-- 
Steven

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web