PyCharm does not find submodules, but console does - python

I cannot properly access submodules in pycharm. Let's say I have
import numpy as np
And then I tried accessing the testing submodule as np.testing.assert_all_close(). The code runs, but pycharm will not recognise the testing submodule, or do any kind of autocompletion. If I import like this however, it works:
import numpy as np
import numpy.testing
Then it works as np.testing.... This did not happen before, maybe it is an interpreter issue? I am using poetry as environment:
https://plugins.jetbrains.com/plugin/14307-poetry
If I open an iPython console using this same environment, there is no issue, autocomplete works perfectly.

Related

Jupyter autoreload workflow

I'm trying to understand the best workflow for impotring script files into a jupyter notebook.
I have a notebook that does somethig like:
%load_ext autoreload
%autoreload 2
import functions as F
Inside functions.py, I further do imports such as
import numpy as np
import mymodule
It seems then that, for example, numpy will get reloaded every time I execute a cell, which makes things a bit slow. How could I automatically reload functions.py without reloading the imports there that I never change?
I don't quite understand your question. The main functionality of %autoreload is to automatically reload modules, what it does, according to you. You can read about it here, I find it pretty well explained.
However, if you need to access import internals, you should take a look at importlib and especially importlib.reload():
import importlib
importlib.reload(my_module)
or
from importlib import reload
reload(my_module)
It is available starting from Python 3.1.
This can be achieved by specifying the configuration option(%autoreload 1) to auto-reload a selected module.
However, the module must be imported as %aimport my_module.

python module import so slow in other IDE but in Spyder it's 20 times faster

I am learning data science, using Python and I suddenly found it take so much time to load these lib like numpy,pandas when I use Pycharm or sublime text3.
But when I use Spyder which has been installed by Anaconda to run my program, it's super fast!
I really want to use Pycharm, how to make it as fast as Spyder?
I also found Spyder will run several Python.exe, Spyder will run 5 python.exe background while sublime just one. Is it the reason that it's so fast?
For example to import these libs:
[import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
from scipy.stats import norm
from sklearn.preprocessing import StandardScaler
from scipy import stats
import warnings
import sympy
]
Then it will take:
sublime 4.1 seconds
Spyder 0.06 seconds
You can use Jupiter Notebooks directly in Pycharm.
See here.
As far as my understanding goes, Spyder uses a Jupyter kernel so it does not have to reload all your imports all the time. Spyder runs the kernel in the background, where Pycharm starts a new Python instance each time you hit run. Each one of those has to load all the dependencies again.

no module named sys and time

I wrote a GUI, for which I used these imports:
import os
import sys
import serial
import scipy
import string
import time
import datetime
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
from collections import deque
from numpy import array
from pylab import xlabel, ylabel, subplot
from scipy.fftpack import fft
from pylab import *
There is a red line below sys and time, I am using pycharm community edition 4.5.3, it is showing the reason for this error is 'no module named sys' and same for time.
But when i tried to run it, it works perfectly.
What is the reason behind it and will it affect my code in future?
Change the python interpreter from python to python2.7. It's helped me.
This is something I just ran into with Intellij 2017.3.4 where it could find every module except sys and time in the editor, but everything would run fine. I had both 2.7 and 3.5 version of python and it did not seem to matter which one I selected as the SDK. I tried adding and removing them.
When I went to Project Structure -> Platform Settings -> SDKs -> Python 3.5 -> Packages it prompted a warning that Python packaging tools not found. and had an install link. I installed it and the editor no longer complained about sys and time. When I switched SDKs to 2.7 (without installing the packaging tools) it complained again.
So I am not exactly sure what is happening that that seemed to fix it for me it other people run into this problem.

Importing issues with Python Modules

I was having issues getting the scikit-learn module to import into python. It would import into the python shell, but not when I did it through my IDE. After reading lots of things online, I got it to work by using:
import sys
sys.path.append(r"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages")
import sklearn
Does anyone have a suggestion so that I don't need to do the sys thing every time I want to use the module?
In your IDE, find where you can change the "Python interpreter" setting, and point it to /Library/Frameworks/Python.framework/Versions/2.7/bin/python

scons cannot import numpy module

I have a sconstruct file and i am trying build a process.
A part of my code is below.
# Import modules needed by Scons
import os
import sys
# Create an Scons Environment
env = DefaultEnvironment()
env.Decider('MD5-timestamp')
sys.path.append(r"C:\Python27\Lib\site-packages")
sys.path.append(r"C:\Python27\Lib\site-packages\numpy")
sys.path.append(r"C:\Python27\Lib\site-packages\numpy\linalg")
import numpy
When i try to run scons, it complain about not able to find some sub module of numpy such as lapack_lite, _umath_linalg. The screenshot of the error attached.
I have checked this files inside my site-pacages. It is defintely present inside the folder.
When i import numpy library from python, i dont have any problem.
I had a dependency issue.
The only solution that worked was to completely remove python, all its libraries.
Reinstall python, libraries and scons back. Made sure all pythonpath and sys path are set properly.
It started to work

Categories