I am consolidating many shell-like operations into a single module. I would then like to be able to do:
pyscript.py:
from shell import *
basename("/path/to/file.ext")
and the shell.py module contains:
shell.py:
from os.path import basename
The problem is that functions imported to the shell module are not available, since the import statement only includes functions, classes, and globals defined in the "shell" module, not ones that are imported.
Is there any way to get around this?
Are you sure you're not just using the wrong syntax? This works for me in 2.6.
from shell import *
basename("/path/to/file.ext")
shell.py:
from os.path import basename
It's not a bug, it's a feature :-)
If you import m1 in a module m2 and then import m2 into another module, it will only import things from m2, not from m1. This is to prevent namespace pollution.
You could do this:
shell.py:
import os.path
basename = os.path.basename
Then, in pyscript.py, you can do this:
from shell import * # warning: bad style!
basename(...)
Related
Is there another way to write it other than doing it in two separate lines like this?
from os.path import isfile, isdir
from os import scandir # or import os.scandir
TL;DR You can't. You got only that method from os.path import <stuff> if you wanna import the functions/objects from inside of os.path.
When you import stuff using just import you can import only modules (module is a term for python file containing python objects). To import the objects inside of a module you use from to traverse upto that module, and then you use import <whateverObject> to import that object from that file (anything and everything is an object in python, as long as it's inside a .py file). The . you use is (generally) to traverse through the directories ('sub' packages?) inside of a package.
How does the interpreter know which directory to include in a package, or how to recognise a given directory as a package? It looks for an __init__.py file inside it. If it finds one, it is a package, and thus you can import it.
import is limited to accessing directories and modules at most, if used alone. When you use from <module_or_directory> import <objects>, the task of accessing directories and/or modules is handed over to the clause after from, and the clause after import takes over the task of looking for python objects inside of the module. You see, these are two distinct things - 1) accessing a file/directory, which comes under file system, and 2) accessing the contents of a file, (specifically, a .py file), which comes under python's domain - neatly separated in the from-import style of importing stuff.
In the case of the os module, os.path is another .pyi file (ntpath.pyi on Windows) that is alias-ed in the os.py file as path. Since it is a module, it goes in the clause after from in from os.path import isfile, isdir. Whereas scandir is a function in os module, hence it goes in the clause after import in from os import scandir.
so I'm new to Python and Flask and I'm currently playing around with some CRUD-statements within Flask/Python
I want to know if I fully understand what's going on but I'm a little bit unsecure regarding the following topic: Modules, Packages import
I want to connect to my SQLite database with Flask. Doing so, I have to do some imports:
import os
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
First thing after the imports are done is to set a basedirectory (=basedir):
basedir = os.path.abspath(os.path.dirname(__file__))
And regarding those steps I have some questions:
Question:
import os
from flask import Flask
Does the first import ("import os") mean that I'm only using a Module called "os"? It's a standalone .py - "file" including a class, some attributes and methods, right?
Does the second import ("from flask import Flask") mean that I'm using the package "flask" and import the module "Flask"? If, e.g., there would be another import like "render_template", does that mean I'm using this module or is it a method from the module "Flask"?
Second question:
basedir = os.path.abspath(os.path.dirname(__file__))
I'd like to understand this code. First of all, I'm declaring a variable called basedir. Then I am going to set the value of that variable to the absolute path for the current .py-script. Now to the single steps:
os => means that I'm using the already imported module "os", right?
path => means that I'm using an attribute from that module?
abspath => means that I'm using a method within the "os" module called "abspath(value)"?
The next thing would be clear if I get an answer to the other things: "
(os.path.dirname(__filename__))
__filename__ => that's a built-in Python attribute, right?
Does the first import ("import os") mean that I'm only using a Module called "os"?
As the statement implies, you're importing the OS module, so you can use the functions in the os module in your python script.
So, now you can make os.function() statements in your script. The OS module is installed with Python by default. Here is info on the os module.
Does the second import ("from flask import Flask") mean that I'm using the package "flask" and import the module "Flask"? If, e.g., there would be another import like "render_template", does that mean I'm using this module or is it a method from the module "Flask"?
This can be confusing since the function name and the import statement have the same name. You're only importing the function flask from the module Flask, not all the functions present in the Flask module.
This can be done for multiple reasons. On is to simplify calling the function. Another could be to save system resources, since you're only
os => means that I'm using the already imported module "os", right? path => means that I'm using an attribute from that module? abspath => means that I'm using a method within the "os" module called "abspath(value)"?
Exactly, read the docs for an explanation by the developers of the module.
Filename
Here is an explanation of the filename usage in Python.
Im gonna answer the first question. Basically when you do just an import, python imports the entire file with all of its modules and functions. Like when you import math you can use math.ceil and other functions. However when you say from math import add you only get a specific module which is ceil like ceil(2.7).
For further details read up here
I want to write a Python module that automatically imports all the good stuff for me (about 50 other modules) so I don't have to copy and past them every time I start a new script. I attempted this by defining the following method in my module, soon to realize when I import my module and call this method, the imports take place locally.
def auto_import():
import os
import sys
# plus 50 other modules...
How can I accomplish this automation using modular programming? (I am using Python 3.6. on Ubuntu.)
You don't need a function to do that, you can simply make a file like commonimports.py which looks like this:
import os
import numpy as np
import sys
#and so on...
And add this import statement in other files
from commonimports import *
And you'll have all the modules ready to use within that namespace
Just make the name of your imported modules global:
def auto_import():
import os
import sys
global os, sys
This is not necessary to use this method if you def auto_import() then every time you have to use a autoimport function whenever you want to use those module.
I'm cleaning up a project that was refactored into smaller .py files. I noticed that a lot of modules are being imported again and again in various files. Some statements are in files that import another which has the same import statement used by the importing file. For example:
main.py
import alt
print (os.getcwd())
alt.py
import os
The print(os.getcwd()) throws a NameError: name 'os' is not defined. Shouldn't os be part of sys.modules when the import statement is executed in alt.py?
Is it possible to use a module imported by another module that was imported first?
They are available in the following namespace:
import alt
print (alt.os.getcwd())
To answer your immediate question, it is possible but not recommended.
Imports adjust the namespace in which they are made. This means that alt has an attribute os that can be accessed in main as
print(alt.os.getcwd())
This is not the recommended way, however, since it makes it less clear which actual os module you are using. You should do import os directly in main. This will not create a separate module object, so do not worry about cluttering your memory with duplicate modules.
The first time an import is encountered during runtime, it is added to the dictionary sys.modules, keyed by the fully qualified name of the module. Future import statements will look in sys.modules for an existing reference before doing any actual work.
In your case, import alt will create a module referenced by sys.modules['alt'] as well as by the name alt in main. The statement import os in alt will be run next. It will create a module referenced by sys.modules['os'] and alt.os. If you were to add a line import os in main after import alt, it will not create and load another module object. Instead, the name os will be bound to the same object pointed to by sys.modules['os'].
The following three versions of main will all call the same getcwd function:
Direct import (recommended):
import alt
import os
print(os.getcwd())
Use the reference in alt (harder to read/trace):
import alt
print(alt.os.getcwd())
Using sys.modules (really not recommended for production unless you know what you are doing):
import alt # necessary to trigger the actual import
import sys
print(sys.modules['os'].getcwd())
All imports load a full module. This even applies to imports of the form from os import getcwd. The module sys.modules['os'] is still created. The only difference is that the importing namespace will only have access to the name getcwd, not os. In your case, if alt contained from os import getcwd instead of import os, the three access methods would change as follows:
Unchanged.
print(alt.getcwd()) since alt.os no longer exists.
Unchanged.
You are importing os only in the in the submodul level of alt. os is to say so only available by access through alt.os. A way around this would be to import all from alt as from alt import *, but this is not what you should do...
As a general rule of thumb, you should re-import your directly called-to modules on the top level of the module that you currently process.
thus at main.py:
import os
and everything is fine
You write it this way
__author__ = 'kerberos'
__date__ = '2017/10/25 20:48 '
import alt
print(alt.os.getcwd())
This is the result:
C:\Users\Administrator\Desktop\11
I have a directory I add to sys.path to import custom modules. What is the correct/best way to use import, from import and sys.path together? What I mean is if it acceptable to use sys.path.append in between the "imports".
For example:
#!C:/Python27
import sys
sys.path.append('C:\\Users\\user\\myPythonModules')
import writedata as wd
import os
import csv
from collections import defaultdict
Edit:
I should have mentioned that writedata would be a custom module that I want to import as wd. The module writedata is located in C:\\Users\\user\\myPythonModules
Yes, it is. There is no syntax or semantic rule in the language that prevents that.
I am not aware of any "style" rule that you may be breaking, but in any case, another option is providing PYTHONPATH to the python interpreter.