I am new to python and I am running into some troubles importing modules. I have very limited prior knowledge to programming (bit of Fortran and C) and I usually work with Matlab.
I searched the web for import methods and the preferred method according to most tutorials is: import X
If I use the console (iPython respectively) I can execute the following:
import scipy
scipy.stats.cauchy.pdf
However when I use the same line in a function it does not work, I have to use
import scipy.stats as s
s.stats.cauchy.pdf
or I get: AttributeError: 'module' object has no attribute 'stats'
By reading this 'import module' or 'from module import' I got the impression that both should work.
If someone could quickly clear that up for me I would be very thankful!
Best Wishes,
Chris
It should not happen on a newly opened ipython instance. You would not normally be able to access like this:
import scipy
scipy.stats.cauchy.pdf
Without getting AttributeError, in either ipython or regular python interpreter. You would need to do
import scipy.stats
somewhere, because it is a submodule.
Either you have an earlier imported scipy.stats hanging around in your ipython namespace, or you have some automatic startup script importing it when ipython starts.
Note: if you are using ipython's run magic function to execute a script, any global variables and imported modules will remain in scope.
Related
I use VS Code to write and test python scripts.
Is it possible to make the editor aware of imported modules
to avoid problems listed like
Module 'numpy' has no 'divmod' member
You would update the Python interpreter settings according to where modules have been installed. (bottom right of VS Code)
The IDE being used isn't really relevant because you could invoke /path/to/bin/python, start a REPL, import the same module, and get the same error
Regarding, "numpy has no ... member", based on searching, that is a PyLint issue, not an import issue
How do I get PyLint to recognize numpy members?
Using pycharm with python 3.7. I am using queue.SimpleQueue. The code runs fine, and PyCharm is pointed at the correct interpreter and all that. But with this code:
import queue
Q = queue.SimpleQueue()
I get a warning "Cannot find reference 'SimpleQueue' in 'queue.pyi'".
I do some searching. I hit ctrl-B on the "import queue" statement and it takes me to a file called queue.pyi in the folder helpers/typeshed/stdlib/3/ under the pycharm installation. So apparently instead of the queue.py file in lib/python3.7/ under the python venv, it thinks I'm trying to import this queue.pyi file instead, which I didn't even know existed.
Like I said, the code runs fine, and I can simply add # noinspection PyUnresolvedReferences and the warning goes away, but then the type inferencing and code hints on the variable Q don't work.
Another fix is to instead import _queue and use _queue.SimpleQueue, because apparently in python 3.7 queue.SimpleQueue is implemented in cython and is imported from a cython package _queue. But importing _queue seems hackish and implementation-dependent.
Is there a way to tell PyCharm that import queue means the actual lib/python3.7/queue.py as opposed to whatever helpers/typeshed/stdlib/3/queue.pyi is?
It was fixed in PyCharm 2019.3 https://youtrack.jetbrains.com/issue/PY-31437, could you please try to update?
I'm new with Python and new on Stackoverflow, so please let me know if this question should be posted somewhere else or you need any other info :). But I hope someone can help me out with what seems to be a rather simple mistake...
I'm working with Python in Jupyter Notebook and am trying to create my own module with some selfmade functions/loops that I often use. However, when I try to some of the functions from my module, I get an error related to the import of the built-in module that is used in my own module.
The way I created my own module was by:
creating different blocks of code in a notebook and downloading it
as 'Functions.py' file.
saving this Functions.py file in the folder that i'm currently working in (with another notebook file)
in my current notebook file (where i'm doing my analysis), I import my module with 'import Functions'.
So far, the import of my own module seems to work. However, some of my self-made functions use functions from built-in modules. E.g. my plot_lines() function uses math.ceil() somewhere in the code. Therefore, I imported 'math' in my analysis notebook as well. But when I try to run the function plot_lines() in my notebook, I get the error "NameError: name 'math' is not defined".
I tried to solve this error by adding the code 'import math' to the function in my module as well, but this did not resolve the issue.
So my question is: how can I use functions from built-in Python modules in my own modules?
Thanks so much in advance for any help!
If anyone encounters the same issue:
add 'import math' to your own module.
Make sure that you actually reload your adjusted module, e.g. by restarting your kernell!
My question is specific to scikit-learn python module, but I had similar issues with matplotlib as well.
When I want to use sklearn, if I just do 'import sklearn' and then call whatever submodule I need, like ' sklearn.preprocessing.scale()', I get an error
"AttributeError: 'module' object has no attribute 'preprocessing'"
On the other hand, when I do 'from sklearn import preprocessing'
and then use 'preprocessing.scale()' it works normally.
When I use other modules like Numpy, it is sufficient to just 'import numpy' and it works well.
Therefore, I would like to ask if anyone can tell me why is this happening and if I am doing something wrong?
Thanks.
A python package is defined in the __init__.py file inside the directory.
This file determines whether or not submodules are include.
When you do import sklearn python finds the file sklearn/__init__.py and executes it to create the sklearn module. This object is the bound to the name sklearn. Submodules are not implicitly imported by the interpreter.
However when doing from sklearn import preprocessing python will first load the sklearn module as before. Then it will check if preprocessing is an attribute of that module (e.g. a function), and if not it will look for the file sklearn/preprocessing.py and improt that module too.
It happens that numpy does something like the following in its __init__.py file:
import .random
Thus when importing numpy as import numpy the execution of that module triggers the importing of numpy.random which is then added as an attribute.
This is useful because sometimes you want to use only part of a package and loading all of it could take a significant amount of time. For example importing numpy does take something like half a second. This is time wasted if you only need a very small subset of its functionality.
You may be interested in reading the documentation for packages.
Numpy conveniently imports its submodules in its __init__.py file and adds them to __all__. There's not much you can do about it when using a library - it either does it or not. sklearn apparently doesn't.
I'm afraid this will a question for a very particular case. At university we have been told to generate documentation by using pydoc. The problem is that we need to create it for a Maya script, and pydoc yells when it finds import maya.cmds as cmds
So, I tried to comment this line but I keep getting errors:
python C:\Python27\Lib\pydoc.py Script.py
problem in Script - <type 'exceptions.NameError'>: global name 'cmds' is not defined
I also tried Script, without the extension .py but it's silly doing that, we still running around the same issue.
Does anybody know how to generate documentation for Maya scripts, where import maya only works in the maya interpreter?
maya.commands is an stub module until it's run inside a working maya environment; if you just import it and inspect it outside of Maya you'll see that it's basically a placeholder.
If you want to inspect the contents, you can import the maya.standalone module and initialize it before running other commands (in this case it means you won't be able to run pydoc standalone.
You can get the documentation using the write command:
import maya.standalone
maya.standalone.initialize()
import pydoc
import mymodule
pydoc.write(mymodule) # writes the mymodule.html to current directory
Be warned, however, that the documentation for all maya built in functions will be unhelful:
'built-in function ls'
however you can at least document your own stuff without the maya parts crashing.
Pydoc, ironically, does not have a lot of external documentation. However you can see the code here:http://hg.python.org/cpython/file/2.7/Lib/pydoc.py (i'm not sure about the delta between this and the 2.6 version for maya pre-2014 but it works as above in maya 2011)