I discovered the phenomenon that always I enter the command
from nltk import *
the help command is not working anymore. I get the following error message then:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'module' object is not callable
Does anybody has an explanation for that phenomenon?
I am writing code in Visual Studio in Python 3.
Thanks and best wishes,
Marcus
Firstly, importing * from nltk is a bad idea. You pollute your namespace with many variables that are unknown/unclear to you.
$ python
# Native Python variables.
>>> vars()
{'__builtins__': <module '__builtin__' (built-in)>, '__name__': '__main__', '__doc__': None, '__package__': None}
>>> len(vars())
4
# After importing from *
>>> from nltk import *
>>> len(vars())
510
Next in Python, modules are not callable but functions are.
From https://docs.python.org/3/tutorial/modules.html
If you quit from the Python interpreter and enter it again, the definitions you have made (functions and variables) are lost. Therefore, if you want to write a somewhat longer program, you are better off using a text editor to prepare the input for the interpreter and running it with that file as input instead. This is known as creating a script. As your program gets longer, you may want to split it into several files for easier maintenance. You may also want to use a handy function that you’ve written in several programs without copying its definition into each program.
To support this, Python has a way to put definitions in a file and use them in a script or in an interactive instance of the interpreter. Such a file is called a module; definitions from a module can be imported into other modules or into the main module (the collection of variables that you have access to in a script executed at the top level and in calculator mode).
A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended. Within a module, the module’s name (as a string) is available as the value of the global variable name.
Looking at the nltk.help module:
>>> from nltk import help
>>> type(help)
<type 'module'>
# A module is not callable.
>>> help()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'module' object is not callable
# A module contains definitions and statements.
>>> dir(help)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', '_format_tagset', '_print_entries', 'brown_tagset', 'claws5_tagset', 'load', 'print_function', 're', 'upenn_tagset', 'wrap']
# A function is callable.
>>> type(help.brown_tagset)
<type 'function'>
>>> help.brown_tagset()
(: opening parenthesis
(
): closing parenthesis
)
*: negator
not n't
,: comma
,
--: dash
yada yada
Not directly related, but if your plan is to import all of nltk, just use import nltk. No need for the *
Looking at the nltk.help module, you would need to use one of the functions defined there. As nltk.help itself is not a function but a library location.
See: https://www.nltk.org/api/nltk.html?highlight=help#module-nltk.help
So if that is the module you want to use, try:
import nltk
nltk.help.upenn_tagset()
Related
Setup: PyCharm, Python 3.10
We have the naming convention to name our python unittest files like an URL. For example: my.domain.org.py
In the past, this was no issue. Now after an IDE and Python Update it does not run anymore. Selecting right click -> Run "Python tests in my.domain.org.py" throws the error:
Traceback (most recent call last):
File "D:\programs\Python\3.10.2\lib\unittest\loader.py", line 154, in loadTestsFromName
module = __import__(module_name)
ModuleNotFoundError: No module named 'my'
It seems, the loader is interpreting the "." in the filename as path.
How can I run the unittest without renaming the file (which solves the issue)?
you cannot import python files with invalid names (in your case has dots in it) directly, but there's a turn around, you can use the imp library like (here in the example I have a function named print_smth that prints "it works!" in the my.file.py):
import imp
with open('my.file.py', 'rb') as fp:
my_file = imp.load_module(
'my_file', fp, 'my.file.py',
('.py', 'rb', imp.PY_SOURCE)
)
if __name__ == "__main__":
my_file.print_smth()
output:
test.py:1: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
import imp
it works!
P.S: preferably DO NOT DO THAT! it is highly deprecated!
I have a script that is using CPython and has pywin32 modules win32gui, and win32con. I have a variable that I want to import from this script into a different PyPy script to use its value.
It looks something like this:
from functions import var1
however when I try to run this code I get the error
Traceback (most recent call last):
File "d:/folder/functions.py", line 5, in <module>
import win32gui, win32con
ModuleNotFoundError: No module named 'win32gui'
All I need is the value of the variable which is an integer, in any way possible. I think I could perhaps make a function that wraps the value of the variable and send it to the script that way, but I think that might be inefficient.
Is there someway, to avoid module conflicts like this when using different interpreters for different scripts?
You can do something like
try:
import win32gui, win32con
except ImportError:
win32gui = None
wn32con = None
then later in your code, where you use these imports, check if they are None first.
I am not able to import the zipline.transforms module
>>> from zipline.transforms import batch_transform
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'zipline.transforms'
Disclaimer: I'm currently a maintainer of Zipline.
I'm guessing the reason you're seeing this error is because that particular module was removed a while back (assuming you're using zipline 1.0.0 or later). If you want to do things similar to transforms you'll need to call data.history() to get your pricing data, and call numpy/pandas functions like .avg() or .std(), or use talib.
I think you should firstly print out your sys.path (print sys.path), and then see where you zipline module is installed (somewhere like .../lib/python2.7/site-packages/zipline). Usually "no module named XXX" is caused by you sys.path doesn't contain the path you installed zipline. You should just add your zipline path into sys.path. Also use anaconda is good for zipline (http://www.zipline.io/install.html), so as to keep the environment tidy and clean.
Why Python 3.4 standard libraries don’t work in Sublime Text?
Running (example in Mark Lutz book) in myth.py in SublimeText:
import math
math.pi
math.sqrt(9)
and get
Traceback (most recent call last):
File "/Users/*myusername*/Desktop/python/math.py", line 1, in <module>
import math
File "/Users/*myusername*/Desktop/python/math.py", line 2, in <module>
math.pi
AttributeError: 'module' object has no attribute ‘pi'
Do it in terminal or EDLE and all works fine!
Try renaming your file to something besides math.py. You are trying to import the math module, and Python is choking because it's trying to import itself, and there's no pi() function defined. If you change your filename to something like py34_math_test.py you should be all set.
I am looking at using setup.py to automatically generate python 3 code from python 2 sources using the 'use_2to3' attribute. My setup.py script includes the following statement:
VERSION = None
with file('version','rt') as FF:
VERSION = FF.read().lstrip().rstrip()
print("VERSION %s" % (VERSION) )
When I type 'python3 setup.py build' I get the error:
Traceback (most recent call last):
File "setup.py", line 18, in <module>
with file('version','rt') as FF:
NameError: name 'file' is not defined
which I understand is correct as the file object no longer exists and I should change it to 'open()'.
The concern is that the '2to3' utility does not detect this and leaves the code untouched.
I unfortunately use this idiom throughout my code.
Is this a bug in '2to3' ?
Use the open instead of the file.
It was tempting to use the file() instead of the open() in Python 2.x -- especially for those with OO background. The file() call resembled calling a constructor for creation of a file object. However, it was always recommended to use open() function instead. This may be the reason why 2to3 does not solve the case.
In Python 3, the file is unknown. The file objects are of the _io.TextIOWrapper class:
>>> f = open('a.txt', 'w')
>>> type(f)
<class '_io.TextIOWrapper'>
>>> f.__class__.__name__
'TextIOWrapper'