1

My file structure looks like:

enter image description here

From deepxi/lib/dev/acoustic/feat/polar.py, I want to import deepxi/lib/dev/acoustic/analysis_synthesis/polar.py, which I managed to do with:

import sys
sys.path.insert(0, '../..')

from ..analysis_synthesis import polar

But I also want to include deepxi/lib/dev/add_noise.py. I've tried from add_noise import add_noise_batch, but to no avail. Any ideas?

I don't have the ability to do export PYTHONPATH=...

Shamoon
  • 41,293
  • 91
  • 306
  • 570
  • 1
    what about `from .. import add_noise`? – Dschoni Dec 18 '19 at 14:13
  • 1
    Does this answer your question? [Importing modules from parent folder](https://stackoverflow.com/questions/714063/importing-modules-from-parent-folder) – Dschoni Dec 18 '19 at 14:15

1 Answers1

1

If you add the root dir for your project as export PYTHONPATH='/path/to/deepxi'

And then to call polar you would do

from lib.dev.acoustic.analysis_synthesis import polar

and to call add_noise_batch you would do

from lib.dev import add_noise_batch

OR

export PYTHONPATH='/path/to/deepxi/lib'

And do:

from dev.acoustic.analysis_synthesis import polar

from dev import add_noise_batch

Like that you can add more to your PYTHONPATH, but best practice is to add root folder (first method) and then use absolute paths to whatever you are calling.

Also, rename lib since that is also a common module within Python. Always name stuff so that it doesn't coincide with an already existing module. So analysis_synth_lib or something that isn't lib

NOTE

You can add multiple paths to your PYTHONPATH, not advised, but you can do! e.g. export PYTHONPATH='/path/to/deepxi:/path/to/deepxi/lib'

Now whenever you run the function, it will exhaust all paths in your PYTHONPATH to find what it's looking for!

Community
  • 1
  • 1
DUDANF
  • 2,618
  • 1
  • 12
  • 42
  • Tried: ``` import sys import os from pathlib import Path IMPORT_PATH = Path(__file__).parent.parent.parent.parent print('IMPORT_PATH', IMPORT_PATH) sys.path.append(IMPORT_PATH) from dev.acoustic.analysis_synthesis import polar import tensorflow as tf from dev.add_noise import add_noise_batch from dev.num_frames import num_frames from dev.utils import log10 ```, but no dice – Shamoon Dec 18 '19 at 14:30
  • I am so confused. Simply, 1. Update PYTHONPATH: `export PYTHONPATH='/path/to/deepxi'` and 2. Add this to the file where you are using noise_batch: `from lib.dev import add_noise_batch` Why are you importing `path` or `sys`? You overcomplicated it. – DUDANF Dec 18 '19 at 14:32
  • I don't have access to `export PYTHONPATH` necessarily on the deployment machine – Shamoon Dec 18 '19 at 14:33
  • ...... Then do `import os` and `os.environ['PYTHONPATH'] = '/path/to/deepxi'`, should work? – DUDANF Dec 18 '19 at 14:34
  • Trying:`IMPORT_PATH = Path(__file__).parent.parent.parent.parent print('IMPORT_PATH', IMPORT_PATH) os.environ['PYTHONPATH'] = IMPORT_PATH` Which errors with `TypeError: str expected, not PosixPath` – Shamoon Dec 18 '19 at 14:37
  • 1
    Ah. Now I see your problem. Don't know the path to begin with! `abs_path = os.path.dirname(os.path.abspath(__file__))` You can use this to get the absolute path? Maybe just print it out for yourself, then use the absolute path directly without keeping `import PATH` and all in there. That's super confusing. Could work tho! – DUDANF Dec 18 '19 at 14:40
  • So close... `IMPORT_PATH = os.path.abspath(os.path.join(os.path.dirname( __file__ ), '..', '..', '..')) print('IMPORT_PATH', IMPORT_PATH) os.environ['PYTHONPATH'] = IMPORT_PATH from dev.acoustic.analysis_synthesis import polar` Errors with `ModuleNotFoundError: No module named 'dev'` – Shamoon Dec 18 '19 at 14:50
  • Ah. Maybe add `from lib.dev...` or even try `from deepxi.lib.dev`? With the importing of PATHs it has become a bit of a hit and try! – DUDANF Dec 18 '19 at 14:54
  • @mirana do NOT provide two path for the same packages (your "PYTHONPATH='/path/to/deepxi:/path/to/deepxi/lib'" thing) - it's __garanteed__ to break things in very interesting ways. You want to ever only use the highest possible path. – bruno desthuilliers Dec 18 '19 at 14:57
  • @brunodesthuilliers yeh, that's very correct, I specially added 'not advised' for this reason. I just added that in to the answer to help the OP understand how it worked. But the OP has a different issue, and cannot access the server and has to find a workaround. – DUDANF Dec 18 '19 at 14:58
  • 1
    @Shamoon don't mess with PYTHON_PATH (or `sys.path`) from within your Python code (unless you really understand what you're doing and why of course, but that's obviously not the case here). – bruno desthuilliers Dec 18 '19 at 14:58
  • @brunodesthuilliers why are people so against messing with the `PYTHONPATH`? I feel if you have a system, which is devoted to one project, then the right thing to do is to include that project in the `PYTHONPATH`? Asking to learn and clear my misconceptions. – DUDANF Dec 18 '19 at 15:00
  • @mirana stating that it's "not advised" is not enough - just don't suggest doing so. Python's import / path system is already enough of a mess for not inducing newbies into doing the worst thing they could. – bruno desthuilliers Dec 18 '19 at 15:00
  • @brunodesthuilliers so why not mess with pythonpath? What could go wrong? – DUDANF Dec 18 '19 at 15:02
  • 1
    @mirana "why are people so against messing with the PYTHONPATH" => note that I said "from within your python code" and "unless you really understand what you're doing and why". Adding to your PYTHON_PATH from your shell (bashrc, virtualenv etc) is perfectly ok (even if usually not the best solution). – bruno desthuilliers Dec 18 '19 at 15:03
  • @brunodesthuilliers ah okay. Yes you're very correct!! I feel like the only option for OP is to set the path in the code. – DUDANF Dec 18 '19 at 15:04
  • What could go wrong ? Well, breaking some other expectations about what's in the path and in which order. This is environment specific stuff, and your script / lib / app has no way of knowing how the whole env has been set up nor why. – bruno desthuilliers Dec 18 '19 at 15:06
  • So what are my options if not to set the `PYTHONPATH`? – Shamoon Dec 18 '19 at 15:40
  • @brunodesthuilliers what should I do if I don't mess with the `PYTHON_PATH`? – Shamoon Dec 18 '19 at 18:03
  • @Shamoon what I meant is that it's better to avoid messing with this in your python code - as a general rule, for code that is to be deployed on different environments etc. And adding to your pythonpath in your bashrc or venv etc is ok, that's actually what envvars are for. – bruno desthuilliers Dec 18 '19 at 20:17