Packages not working, using Anaconda - python

I have installed Anaconda for Windows. It's on my work PC, so I chose the option "Just for Me" as I don't have admin rights.
Anaconda is installed on the following directory:
c:\Users\huf069\AppData\Local\Continuum\Anaconda
The Windows installer has added this directory (+ the Anaconda\Scripts directory) to the System path.
I can launch Python but trying to run
x = randn(100,100)
gives me a Name Error: name 'randn' is not defined,
whereas, as I understood, this command should work when using Anaconda, as the numpy package is included.
it works fine if I do:
import numpy
numpy.random.randn(100,100)
Anyone understand what could be happening ?

I can launch Python, but trying to run x = randn(100,100) gives me a Name Error: name 'randn' is not defined, whereas, as I understood, this command should work when using Anaconda, as the numpy package is included
The Anaconda distribution comes with the numpy package included, but still you'll need to import the package. If you want to use the randn() function without having to call the complete name, you can import it to your local namespace:
from numpy.random import randn
x = randn(100,100)
Otherwise, the call numpy.random.randn is your way to go.
You might want tot take a look at the Modules section of the Python Tutorial.

Related

VSCode - import errors and interpreter errors

I have several imports in my current code:
from flask import Flask
import datetime as dt
import numpy as np
import pandas as pd
When I run this in VSCODE I am getting this error:
However, running this in jupyter notebook has no problems. When I looked online it said to use python interpreter but when I go to do that I get this error:
And another error:
Anaconda prompt says modules/packages are installed but when I run pip install in default windows terminal it says pip has no module:
Delete and reinstall Python extensions according to the same problem on github.
The second problem is related to the location of your Python interpreter. You need to choose the correct interpreter. I still recommend using one Python version in one environment. You can use other Python versions in virtual environment, so it won't lead to confusion.

So i installed numpy . But when i call it in a program an error occurs. Any method to solve it permanently in windows 10

Here is the error
import numpy
Exception has occurred: ModuleNotFoundError
No module named 'numpy'
File "C:\path\to\file\32.py", line 1, in <module>
import numpy
Let me know how did you install the NumPy package; using pip or something else?
If you have multiple python versions, i.e. 2.x and 3.x at the same time, please make sure your interpreter for the 32.py file is the version that you installed NumPy on.
To possibly fix your problem, you should first try installing it and see if there are any errors. You should also check the version of Python you are running on Windows 10, because when you update Python it sometimes switches names between py and python
As you can see, the version of Python has changed between py and python so you should try changing that first.
If this does not work, you should try finding the directory for NumPy and adding it to the system PATH in your script. The installer usually shows you the location by doing the following:
import sys
sys.path.append("<insert numpy location here>")
import NumPy
This should manually force it into finding the package. If none of this works, please tell us and we should be able to find a different solution.
Happy Coding!
If you're using a code editor like PyCharm, you could install it by clicking on
file then settings then the project interpreter setting and install new module! You can search for the module and install.
Make sure that the python version that you want to use is a Windows Environmental Variable. You can test this by running this line in your command line.
> python --version
If you get some other python version that is not the one that you wish to use you can set the python version you want by finding where exactly your Python folder is located and go into settings and set the path as a new variable (I can leave a tutorial for you). If that is too much of a hassle, the Python installers can set the python that you will install as an environmental variable for you. (You could uninstall and reinstall and make sure that you allow it to make it an environmental variable.
After that you should be able to import whatever external packages you want using pip
for example:
pip install numpy

How to import pandas using R studio

So, just to be clear, I'm very new to python coding... so I'm not exactly sure what's going wrong.
Yesterday, whilst following a tutorial on calling python from R, I successfully installed and used several python packages (e.g., NumPy, pandas, matplotlib etc).
But today, when trying to run the exact same code, I'm getting an error when trying to import pandas (NumPy is importing without any errors). The error states:
ModuleNotFoundError: No module named 'pandas'
I'm not sure what's going on!?
I'm using R-Studio (running on a Mac)... here's a code snippet of how I'm doing it:
library(reticulate)
os <- import("os") # Setting directory
os$getcwd()
repl_python() #used to make it interactive
import numpy as np. # Load numpy package
import pandas as pd # Load pandas package
At this point, it's throwing me an error. I've tried googling the answer and searching here, but to no avail.
Any suggestions as to how I'd fix this problem, or what is going on?
Thanks
Possibly your python path for reticulate changed upon reloading Rstudio. Here is how to set the path manually (filepath for Linux or Mac):
library(reticulate)
path_to_python <- "~/anaconda3/bin/python"
use_python(path_to_python)
https://stackoverflow.com/a/45891929/4549682
You can check your Python path with py_config(): https://rstudio.github.io/reticulate/articles/versions.html#configuration-info
I recommend using Anaconda for your Python distribution (you might have to use Anaconda anyway for reticulate, not sure). Download it from here: https://www.anaconda.com/distribution/#download-section
Then you can create the environment for reticulate to use:
conda_create('r-reticulate', packages = "python=3.5")
I use Python 3.5 for some specific packages, but you can change that version or leave it as just 'python' for the latest version.
https://www.rdocumentation.org/packages/reticulate/versions/1.10/topics/conda-tools
Then you want to install the packages you need (if they aren't already) with
conda_install('re-reticulate', packages = 'numpy')
The way I use something like numpy is
np <- import('numpy')
np$arange(10)
You need to set the second argument of the function use_python, so it should be:
For example, use_python("/users/my_user/Anaconda3/python.exe",required = TRUE)
DON'T forget required = TRUE

Error in sys.path.insert to reference a github module

I am trying to use a module from github called ffn and have downloaded it to my desktop. I am using Jupyter notebook for Python and was told i need to insert this code:
import sys
sys.path.insert(0, r"C:\Users\...\Desktop\ffn-master\ffn\_core_.py")
import ffn
..in order to use this code.
However when i run the subsequent lines of code that call functions from here I still get the error:
C:\ProgramData\Anaconda3\lib\site-packages\ffn\core.py:2054:
RuntimeWarning: invalid value encountered in minimum
negative_returns = np.minimum(returns, 0.)
I restarted the kernel and everything but still get the same error.
Can anyone help?
"The desktop module has the fixes that the installed module in the anaconda path does not." - It seems like you still have the original module installed in the anaconda library.
try running
conda uninstall ffn
and then include the entire library in the path in you notebook with
sys.path.append(os.path.abspath(r"C:\Users\...\Desktop\ffn-master\ffn\")

Python on Windows error: python25.dll not found

I am running Python 2.7 on Windows 7 (on parallels on a Mac running Mountain Lion) and getting a strange error. It has happened both using Python(x,y) and the Enthought Python Distribution (paid version - 64-bit).
Running python from the command line initially works fine (and always does after rebooting the machine).
But, when I try to run my code at the command line as
python the_script.py
On the first try, I get this error window:
After that, I get the same error just from typing python at the command line.
If I specify the path as c:\python27\python the_script.py it works fine.
Here are all the modules I'm loading in my scripts:
import numpy as np
import subprocess as sub
import parallel_condor_Jacobian as pcj
import os
import shutil
In parallel_condor_Jacobian the following modules are loaded:
import numpy as np
import os
import subprocess as sub
Nothing really out of the ordinary I think!
Is one of these packages somehow dependent on python25.dll?
Fixes I have tried include totally removing python 2.7, reinstalling, and removing all python path stuff from my PATH environment variable and replacing them with c:\python27.
I'm really at a loss here. Happy to provide more relevant information.
remove the python.exe in the local folder ... and tell your colleagues to upgrade to at least 2.6 :P
and also tell them that the python exe is not portable :P

Categories

Resources