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


Groups > comp.lang.python > #80557

Re: Python modules

Newsgroups comp.lang.python
Date 2014-11-11 12:06 -0800
References <i0h06a9pj8h3olo5rnrgc64i7ckpvtvti6@4ax.com>
Message-ID <e59e1214-b867-4e7b-9d31-fb8c2efd5d40@googlegroups.com> (permalink)
Subject Re: Python modules
From Will Acheson <willyach07@gmail.com>

Show all headers | View raw


On Sunday, November 9, 2014 11:51:41 PM UTC-5, Steve Hayes wrote:
> I have a book on Python that advocates dividing programs into modules, and
> importing them when needed. 
> 
> I have a question about this.
> 
> I can understand doing that in a compiled language, where different modules
> can be imported from all sorts of places when the program is compiled. 
> 
> But I understand that Python is an interpreted language, and If I wrote a
> program in Python like that, and wanted to run it on another computer, how
> would it find all the modules to import at run-time, unless I copied the whole
> directory structure over to the other computer?
> 
> 
> 
> 
> -- 
> Steve Hayes from Tshwane, South Africa
> Web:  http://www.khanya.org.za/stevesig.htm
> Blog: http://khanya.wordpress.com
> E-mail - see web page, or parse: shayes at dunelm full stop org full stop uk

I have had a lot of trouble with executing relative imports with some of my projects in python.  

Are there any best practices or methods besides '../../' type hard-coding?

In one project, a spider using scrapy, I used this method to import a module from 'the other side' of a file directory. 

----
spider_lib_path = os.path.realpath(os.path.dirname(__file__) + '/../../../../../library/python')
object_builder_printer = imp.load_source('object_builder_printer', spider_lib_path + '/object_builder_printer.py')
object_builder = object_builder_printer.object_builder_printer(settings)
----

the file structure was:

spiders/
-current/
--app/
---spiders/
----scrapy/
-----Charlotte/
------1.0/
-------pipelines.py (the file that needs to import object_builder_printer.py)

--library/
---python/
----object_builder_printer.py


I think the issue had something to do with there being duplicitous file names, as in, there are multiple directories named 'spiders'.



Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Python modules Steve Hayes <hayesstw@telkomsa.net> - 2014-11-10 06:55 +0200
  Re: Python modules Deepfriedice <Deepfriedice@openmailbox.org> - 2014-11-10 14:55 +1000
  Re: Python modules Ben Finney <ben+python@benfinney.id.au> - 2014-11-10 16:12 +1100
    Re: Python modules Steve Hayes <hayesstw@telkomsa.net> - 2014-11-10 08:58 +0200
      Re: Python modules Ben Finney <ben+python@benfinney.id.au> - 2014-11-10 18:48 +1100
  Re: Python modules Roy Smith <roy@panix.com> - 2014-11-10 08:36 -0500
    Re: Python modules Chris Angelico <rosuav@gmail.com> - 2014-11-11 00:49 +1100
  Re: Python modules Will Acheson <willyach07@gmail.com> - 2014-11-11 12:06 -0800
    Re: Python modules Ben Finney <ben+python@benfinney.id.au> - 2014-11-12 09:18 +1100

csiph-web