Přidat otázku mezi oblíbenéZasílat nové odpovědi e-mailem Python - Problém s importem

Ahoj, poradí někdo s importem ?

Mám soubor: /usr/lib/python3/moje_slozka/__init__.py
V tomto souboru mám:

def foo():
  pass

Mám soubor /home/muj_script.py
V tomto souboru mám:

import moje_slozka
moje_slozka.foo()

Dostanu chybu:

AttributeError: 'module' object has no attribute 'foo'

Děkuji za pomoc.

Předmět Autor Datum
Neviem... že by si skúsil importovať nejaký skript .py a nie tvoju zložku?
pme 13.07.2015 18:40
pme
modules.html
MM.. 13.07.2015 18:43
MM..
Že jsem se tady vůbec ptal...
Z55 13.07.2015 19:28
Z55
Takze ani po 2 jasnych odpovediach nevidis co robis zle, ok, programuj dalej, to bude "vytvor". OMG.…
MM.. 13.07.2015 19:48
MM..
Nevim, jestli je to pricina problemu, ale ja jsem zvykly instalovat svoje knihovny do /usr/lib/pyth…
gilhad 14.07.2015 00:09
gilhad
Moze to mat kde chce ked si nastavi cesty. 6.1.2. The Module Search Path When a module named spam… poslední
MM.. 14.07.2015 07:28
MM..

Takze ani po 2 jasnych odpovediach nevidis co robis zle, ok, programuj dalej, to bude "vytvor". OMG. Mali by to zakazat.

use your favorite text editor to create a file called fibo.py
Now enter the Python interpreter and import this module with the following command:
>>> import fibo

Nevim, jestli je to pricina problemu, ale ja jsem zvykly instalovat svoje knihovny do

/usr/lib/python2.7/site-packages/cosi/neco.py (v ramci baliku neco, samozrejme tam mam i __init__.py)

eventualne samostatne do

/usr/lib/python2.7/site-packages/neco.py

a chodi mi to bez problemu.

Moze to mat kde chce ked si nastavi cesty.

6.1.2. The Module Search Path

When a module named spam is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named spam.py in a list of directories given by the variable sys.path. sys.path is initialized from these locations:

the directory containing the input script (or the current directory).
PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).
the installation-dependent default.

After initialization, Python programs can modify sys.path. The directory containing the script being run is placed at the beginning of the search path, ahead of the standard library path. This means that scripts in that directory will be loaded instead of modules of the same name in the library directory. This is an error unless the replacement is intended. See section Standard Modules for more information.

Ale on asi citat nechce. On chce ostat blby.

Pyta sa na to preco sa mu neimportne __init__.py, ten sa ale IMHO importne vtedy ak importne nejaky iny .py z tej zlozky. Je toho plne google (na linku ktory som dal ma o tom celu sekciu 6.4, ma si to citat). P.S> a osobne si myslim ze v __init__.py nemaju byt definovane funkcie, moze si tam drbnut kod ktory inicializuje premenne apod, ale IMHO ne fcie.

Zpět do poradny Odpovědět na původní otázku Nahoru