importing a module from input - input

Forgive a noob. This may be beyond me.
I currently import variables from a module via
from a import *
What I aim to do is import the file as per the input string.
mod=str(input("Select a module: "))
from str(mod) import *
This is what I tried. Clearly wrong. I would like the code to ask for an input, which would be the name of a specific module, then import what the user inputs.
Sorry I can provide any more code, the nature of the question prevents me from being capable of showing what I need

You can simply use __import__():
>>> d = __import__("datetime")
>>> d
<module 'datetime' from 'C:\\Python33\\lib\\datetime.py'>
For a more sophisticated importing I suggest using importlib.
EDIT1 to make it more clear:
>>> mymodule = __import__(input("Which module you want?" ))
>>> mymodule.var1
If you want var1 instead of mymodule.var1, I would make aliases to the global namespace. However, I wouldn't do that since I do not see any sense in that.

Related

RPS: Cannot import name ExporterIFCUtils

With the revitpythonshell 2020 I try to import the class ExporterIFCUtils
from Autodesk.Revit.DB.IFC import ExporterIFCUtils
and get the error:
"Exception : IronPython.Runtime.Exceptions.ImportException: Cannot import name ExporterIFCUtils"
#StefanAnd you'll need to add a reference to the RevitAPIIFC.dll first:
>>> clr.AddReference("RevitAPIIFC")
>>> from Autodesk.Revit.DB.IFC import ExporterIFC
>>> ExporterIFC
<type 'ExporterIFC'>
>>>
It's a bit weird, since it looks like you're importing from a module, but it's a .NET "namespace". These can span multiple assemblies as in the case here, so first referencing the RevitAPIIFC.dll will populate the namespace with the types you expect. Sadly, the Revit API documentation doesn't actually seem to provide the assembly names. At least, I couldn't find them...

list available spaCy language objects

I need to list the currently available spaCy language objects in spacy.lang1. I have tried dir(spacy.lang) and searching the various options with no luck. How can I list the available language objects in the module?
Here is the solution that I found if it helps anyone else. Use spacy.__file__ to get the path for spaCy and then create a list of the directories in spacy/lang.
import spacy
from pathlib import Path
spacy_path = Path(spacy.__file__.replace('__init__.py',''))
spacy_langs = spacy_path / 'lang'
SPACY_LANGS = [str(x).split('/')[-1] for x in spacy_langs.iterdir() if x.is_dir() and str(x).split('/')[-1] != '__pycache__']

Dictionary not defined after module import with this dictionary Python 3.4

I guess, I still don't fully understand scope rules in python (Was sure I do!), so please answer with some explanation.
I wrote module like this:
import pickle
question_dictionary = {}
#few not important definitions of functions
def functionA():
blabla
def functionB():
blabla
#and few classes
def Class1:
blabla
#and here was my last class, maybe important for this problem:
def Class2:
def __init__(self)
blabla
question_dictionary[blabla] = self
So, I import this file in interactive mode. Classes work good, also functions. But when i type in interactive mode question_dictionary I got information, that it's not defined. I don't understand why.
I tied to initialize another dictionary in interactive mode, and it work, the code "dictionary = {}" is valid.
I also tried to comment out the last lines:
question_dictionary[blabla] = self
But I still got problem, "NameError: name 'question_dictionary' is not defined"
Your dictionary is part of the globals of your module. You can reference it as such:
import yourmodule
yourmodule.question_dictionary
Globals are always per module, and your interactive interpreter namespace is really a module too (called __main__). You can create a new reference in that namespace by using from ... import ...:
from yourmodule import question_dictionary
# now question_dictionary is another reference to the same dictionary

how to import one project(.zexp file) to another project?

can any one tell how to solve
TypeError:('object.__new__(X): X is not a type object (classobj)', <function
_reconstructor at 0xb766fa04>, (<class DateTime.DateTime.DateTime at 0x9382d4c>, <type
'object'>, None))
while importing a one project(for ex. brundelre3.zexp file) to another in zmi.
I tried it importing the project(brundelre3.zexp) already in zmi under / ->import/export (tab)-> import file name ->ownership-> selected the radio button of Retain existing ownership information-> import (button) so it worked properly before but its not working now . Can anyone tell whats the reason for my error.
I can only make wild guesses, you probably need to debug it:
Perhaps you have created a type that has a name that clashes with something built-in.

Can't find link using Mechanize follow_link()

I just starting looking at the Python version of Mechanize today. I took most of this code from the first example on http://wwwsearch.sourceforge.net/mechanize/. The documentation of this module is very sparse and I have no idea how to debug this.
I am trying to find and follow the first link with the text "Careers". When I run this I get this error "mechanize._mechanize.LinkNotFoundError". Can anyone tell me what I am doing wrong?
import re
import mechanize
br = mechanize.Browser(factory=mechanize.RobustFactory())
br.open("http://www.amazon.com/")
response1 = br.follow_link(text_regex=r"Careers", nr=1)
assert br.viewing_html()
print br.title()
I just tried the sample code myself, and it looks like the problem is with the nr argument. It's not documented anywhere but in the source code (which is far more informative than the documentation!), and it states that:
nr: matches the nth link that matches all other criteria (default 0)
Because the nr argument is 0-based, when you gave the argument of 1, it was looking for the second mention of Careers, which was obviously nothing.
Because it defaults to 0, or the first link found, you can set the nr argument to 0, or leave it off entirely.