Import error -- No module Named myfile python2.7 - python

Ok I am new to doing these imports in python and calling python files to be imported.
My directory structure looks like this.
dir1
|-__init__.py
|-file1.py
|-dir2
|---|-__init.py__
|---|-file2.py
|---|-file3.py
|---|-dir3
|-----|-__init.py
|-----|-file4.py
I want to import file 2 from dir3 level
This is what I did
from dir1.dir2.file2 import whatever
I get Import error: No module named dir1.dir2.file2
I tried a lot of options to set my python path, but I am not able to get the import working. It just might be an easy fix but am not able to see it. Your suggestions will be much appreciated.

If you're trying to load a file from dir3 level (e.g. file4), you could use relative paths:
from ..file2 import foo
though this is highly discouraged normally.

Related

Import .py file to main file

I tried writing some commenly used function in a seperate file and import the same into mainApp file, but not able to use import.
I did find many questions regarding the this same question but, the solution was to keep the files in the same folder
I tried without .py as well, but the same error:
Can you please help me how can i fix this issue ?
No '.py'. Just import seperate
Try using this in mainApp.py:
from seperate import *
a()
where seperate.py looks like this:
def a():
print('hi')
Well, sorry, those two files need to be in the same folder. This is not a solution to your problem.
The syntax of a relative import depends on the current location as well as the location of the module, package, or object to be imported. Here are a few examples of relative imports:
from .some_module import some_class
from ..some_package import some_function
from . import some_class
Read more about Absolute vs Relative Imports in Python
In your case it should be:
from .seperate import a
Also check this question:
Importing from a relative path in Python
add your project directory into your path variable so that python know from where you want to import file

python package import from the root

I have a following directory structure:
main_work/
data_processing/
data_extraction.py
data_selector.py
data_selector.py:
from data_extraction import connect_to_db
If I start working in main_work and want to do
from data_processing import data_selector as ds
it throws me an error:
ModuleNotFoundError: No module named 'data_extraction'
So, the import in data_selector.py takes into account only current folder (main_work) and doesnt go one level deeper into data_processing.
I need to be able to do from main_work following:
from data_processing import data_selector as ds
and from data_processing following:
import data_selector
How do I achieve those two functionalities?
You may want to add your folder to the PYTHONPATH, this way python will know where to look for the modules you want to import.
You need the following structure
main_work/
data_processing/
__init__.py
data_extraction.py
data_selector.py
and in data_selector.py
from .data_extraction import connect_to_db
the . specifies that it should look in the same directory as the data_selector module

Importing modules from a folder in python27

This sounds ridiculous as there appears to be an unlimited number of responses to this question on this site - but I can't find a straightforward solution without temporarily changing my system path for each reload (or init, which doesn't work for my setup). I'm looking for a secure, non-hacky way of getting this done.
Simply put - I have a directory structure as follows:
**root**
>main.py
>**modules**
>>rivescript.py
>>js.py
>**plugins**
>>weather.py
>>synd.py
To make it simple, I would like to import every available module in the presented subdirectories (modules, plugins) natively in main.py
Pseudo:
#main.py
import "./modules/*.py" as modules_*
import "./plugins/*.py" as plugins_*
And be able to call functions as something like:
plugins_weather.get("3088")
modules_rivescript.RiveScript.reply("localuser", language_input)
Any suggestions? Speed and resource consumption are a big thing for this project.
First, you should put __init__.py files (which could be empty) in modules/ and plugins/ directories, to mark them as packages.
Now, you are able to import your modules in main.py:
import modules.js as js
import modules.rivescript as rivescript
import plugins.weather as weather
import plugins.synd as synd
weather.get("3088") # Usage example

Dynamic Module Python

I have a program that creates a module called "cool" using file operations. I later say import cool and then uses the cool module that was created.
Here is my directory
project/
main.py
modules/
maker.py
cool/ #this folder and its contents was made by maker.py
__init__.py
coolm.py
If I want to make my program into the .exe format, this strategy will not work anymore. Does anyone know another technique?
Note: I cannot use exec to use the cool module..
Import your module when you need it like this:
coolmod = __import__('coolm')
coolm.someproperty
Alternatively you could try:
import importlib
coolmod = importlib.import_module('coolm', 'cool')
This allows you to specify the package name as a second argument.

Python: Unit Testing Module and Relative Imports

Currently have the following file hierarchy:
\package
__init__.py
run_everything.py
\subpackage
__init__.py
work.py
work1.py
work2.py
\test
__init__.py
test_work.py
test_work1.py
My first question is regarding relative imports. Suppose in \subpackage\work.py I have a function called custom_function(), and I would like to test that function in test_work.py. For some reason I can not figure out how to make this import from one module to another. Trying from .. subpackage.work1 import custom_function() does not seem to work, and yields the error Attempted relative import in non-package Is there any way to resolve this?
2)
I would like to run all test files from run_everything.py with one function, would adding a suite() function in each test_work*.py file, which adds each unit_testing class to suite.addTest(unittest.makeSuite(TestClass)), and finally importing them into the top-level run_everything.py be the most conventional way in Python2.7?
Here is a hack*
Insert the path's to "subpackage" and "test" to your python path in run_everything using:
import sys
sys.path.insert(0, '/path/to/package/subpackage')
sys.path.insert(0, '/path/to/package/test')
And then, you can import all your files using vanilla imports in run_everything:
import work, work1, work2
import test_work, test_work1
*This won't permanently affect your PYTHONPATH.

Categories

Resources