Importing PyTorch in PyCharm using Anaconda - python

I just installed PyCharm and Anaconda. I installed PyTorch to Anaconda and i can even use "import torch" in Anaconda. I've created a new Project in PyCharm with the Anaconda Interpreter but i still can't use PyTorch in PyCharm.

I had this problem also. Program that imported torch worked fine at anaconda prompt running in my pytorch env, but when i ran pycharm from the windows shortcut and EVEN set my environment to use pytorch env, it would complain torch could not be imported.
When i ran pycharm from the prompt as shown, it worked.

If you used a bash script and the conda environment to run python codes in your PyCharm IDE.
You also need source activate myenv in your bash script.
Like this:
#!/usr/bin/env bash
source activate myenv
python test.py
source deactivate

I got the same problem and you that how i solved the problem:
1- when you open project choose conda as your project interpreter as shown here
2- in your project go to setting -> project: project name -> Project Interpreter check that you are using Conda Package Manager and your anaconda local settings

I got same problem and solved it:
After you installed anaconda, source activate your own envrionment.
Install pycharm-community and pytorch follow their official instructions.
Start pycharm-community in the same anaconda environment.
Loading your own project, followed instructions as Manuel Lagunas
Finally, you have to install packages of torch in the environment of pycharm: pictures as:
press '+' on the right side to install
you can choose packages of torch by typing 'torch'

Finally solved this in Windows 10 after sinking an entire morning into it.
Create a new Conda environment (via PyCharm or however you fancy)
Navigate to C:\Your\Environment\Path\Scripts in a command terminal
Mine looks like this: C:\ProgramData\Anaconda3\envs\Snek_V2\Scripts
Find what commands you would need for a pip install from the PyTorch website
They'll look something like this:
pip3 install https://download.pytorch.org/whl/cpu/torch-1.0.1-cp37-cp37m-win_amd64.whl
pip3 install torchvision
In the command terminal, you want to enter those commands with one difference; you'll need to replace pip3 with pip.exe. As you've navigated to the exact environment you plan to work in, this will be the pip relevant to you.
e.g. pip.exe install torchvision
Open PyCharm and open the project that's working in the Conda environment we just set up.
If you open up a Python Console, you should now be able to enter import torch and not have any errors pop up in response.

Related

I installed package, but python said No moudel <name-packages> any packages I try to install it

I try using pip and pip3 and python -m pip and all ways to install. The terminal says the packages already installed after the first try to install, but when I try to import the package I had error no moudel name.
I feel the peoblem coming form here
But I am not sure
And when I go to packages in pycharm I saw the packages not installed
I appreciate your help
I'm assuming you're using windows.
It looks like you install the package directly in your system and PyCharm are using a virtual environment to run your code.
Try to activate this virtual environment before run your code:
source venv\Scripts\activate
If you see "(venv)" at the begging of your terminal prompt the virtual environment are activated.
Run pip list to check what packages are installed in there and probably you have to install your package another time, this time in your activated virtual environment. The official documentation will help to understand how and why use virtual environments.
After that you can try to run your code directly from the terminal:
python your_file.py
Install the packages from PyCharm itself, not the terminal as I think pycharm is running a virtual environment.
Maybe you can follow https://www.jetbrains.com/help/pycharm/installing-uninstalling-and-upgrading-packages.html#packages-tool-window

Can't Create a Conda environment using Pycharm

I am having difficulty with creating a new conda (Anaconda 3, python 3.7) environment using Pycharm (edition 2019.2.2.)
I have added the (hopefully) correct Conda.exe file (from Users\...\AppData\Local\Continuum\anaconda3\Scripts\conda.exe) as Conda executable, but the installer does not install the first package (pip-19.2.2) while creating the Conda environment.
Am I doing something wrong?
I have solved the problem with the help of Pycharms support team.
If you are also not able to create a conda environment in Pycharm, first try to run:
conda update python-libarchive-c
in the anaconda command prompt.
If this doesn't work and pycharm is still stuck while making a virtual environment, do the following:
Look into your log: in pycharm go to help -> show log in explorer.
For me PyCharm stumbled upon the following command:
C:\Users\\...\AppData\Local\Continuum\anaconda3\Scripts\conda.exe create -p C:\Users\\...\AppData\Local\Continuum\anaconda3\envs\Project_OpenPNM -y python=3.7
Try running this command directly in the anaconda prompt. Subsequently, activate your environment and add the python interpreter created directly as an excisting interpreter in pycharm.

Jupyter can't find keras' module

I have installed Tensorflow and Keras by Anaconda (on Windows 10), I have created an environment where I am using Python 3.5.2 (the original one in Anaconda was Python 3.6).
When I try to execute import keras as ks, I get ModuleNotFoundError: No module named 'keras'.
I have tried to solve this issue by sys.path.append(C:\\Users\\ ... \\Anaconda3\\python.exe)
with both notebook and console, but I continue to get the same error.
How could I solve this issue?
Please try the following:
Run these in the jupyter notebook cell:
import sys
sys.path
sys.executable
It may not be pointing to your virtual environment but to the root
The fix is to install the jupyter notebook from inside your virtual environment
$ . your_env/bin/activate
(your_env)$ python -m pip install jupyter
Now you can import tensorflow or keras
Jupyter uses iPython under the hood, for python. So when you install Jupyter, it will also install iPython. There was one issue when I installed keras and Jupyter: I already have iPython installed in my root Anaconda environment. This is the output after I install Jupyter and keras:
In [2]: import sys; sys.path
Out[2]:
['/home/user/anaconda3/bin',
'/home/user/anaconda3/lib/python36.zip',
'/home/user/anaconda3/lib/python3.6',
'/home/user/.ipython']
Notice that even though I am inside my conda environment, it still looks for libraries in my root conda environment. And of course keras isn't there.
The step to fix is simply re-activate my environment, with:
source deactivate && source activate [my_env]
Then I am using a correct ipython:
Out[2]:
['/home/user/anaconda3/envs/ml3/bin',
'/home/user/anaconda3/envs/ml3/lib/python36.zip',
'/home/user/anaconda3/envs/ml3/lib/python3.6',
'/home/user/.ipython']
(Not an answer but some troubleshooting hints)
sys.path is not the path to your Python executable, but the path to the libraries.
Check where Keras is installed and check your sys.path. How exactly did you install Keras?
Try to execute the same command from the Python interpreter. Do you have the same issue?
How did you install Jupiter, is the sys.path visible from there the same as sys.path visible from your Python interpreter?
Do Jupiter and Keras use the same version of Python?
You might try to uninstall Jupiter and install it again, and hope that the new installation picks up the packages which are already installed. What could happen is that you have more than one Python installation and different libraries being installed to the different places. sys.path, when requested from different environments, might give you a hint if that's true.
The kernel in console and jupyter are not necessarily the same, and the problem might be that you are not on python 3.5.
python --version
should tell you what is running in the console, and in jupyter you should see it as a choice on starting a new notebook. For me, the information in
Using both Python 2.x and Python 3.x in IPython Notebook
was very helpful.
I have realized that I had two different Jupyter's directories, so I have manually deleted one on them. Finally, I have reinstalled Anaconda. Now Keras works properly.
If you are a windows/mac user who are working on Jupyter notebook “pip install keras” doesn't help you .Try the below steps.It was solved for me
1. In command prompt navigate to the “site packages” directory of your anaconda installed.
2. Now use “conda install tensorflow” and after “conda install keras”
3. Re-start your Jupyter notebook and run the packages.
Acually, I did this command pip install keras and sudo -H pip3 install keras and pip3 install keras. None of them worked. I added the following command and everything worked like a charm:
pip install Keras. Yes a capital 'K'
Here's how I solved this problem.
First, the diagnosis. When I run which python in a terminal window on my Mac (the same terminal I used to launch jupyter, I get /Users/myusername/.conda/envs/myenvname/bin/python, but when I run the same command from a terminal within Jupyter, I get /usr/bin/python. So Jupyter isn't using the correct python executable; the version it's using doesn't have any of my packages installed.
But which jupyter returns /usr/bin/jupyter; it's using a version of jupyter that isn't coming from within my conda environment. I ran conda install jupyter and now which jupyter returns /Users/myusername/.conda/envs/myenvname/bin/jupyter (for some reason I had to restart the terminal window for this to take effect.) Then if I relaunch jupyter notebook, the notebook is using the correct version of Python and I have access to all my installed conda packages. 👍
I had a similar problem.
I added the Conda environment as a new kernel.
First, install ipykernel:
conda install ipykernel
Next, create the kernet:
python -m ipykernel install --user --name tf-gpu --display-name "TensorFlow-GPU"
Now, when you run your notebook, change the kernel to the new one, to "TensorFlow-GPU" in this example.

quandl issue with Pycharm

I have installed quandl via pip through Anaconda Prompt and everything seems good. But then, I can't use quandl on my PyCharm.
I have Python 3.
I have tried several solutions, but none seem to work.
However, quandl works fine in Spyder.
Any suggestion to make it work in PyCharm?
It's very weird because even if I just type import quandl in PyCharm and run it, then I get the quandl has no get attribute error!
Make sure Pycharm is using the Python interpreter you installed quandl on.
In Pycharm the Interpreter Settings are found under
>File, >Settings, >Project:, >Project Interpreter
If import quandl works on python3 (terminal) or in Spider and not in Pycharm, is probably a configuration problem.
You should set the correct Python Interpreter in Pycharm settings.
Follow this path in pyCharm:
File -> Settings -> Project -> Project Interpreter
Then, select which one that contains quandl.
Anyway, I suggest you a different approach: i think that the best solution is set an anaconda environment for each project. In this way you can set the project interpreter with the anaconda environment and all will work well.
To create an environment use the following command:
conda create -n <name> python=x.x anaconda
then activate it with
activate <name> (Windows)
source activate <name> (Linux)
install new packages in this way:
conda install -n <name> <package>
At the end deactivate environment:
source deactivate
I link this tutorial on anaconda environments.

How to install Matplotlib for anaconda 1.9.1 and Python 3.3.4?

I am configuring Anaconda 1.9.1 together with Python 3.3.4 and I am unable to setup Matplotlib for anaconda environment when I try to add package using Pycharm. I also tried to install from Matplotlib.exe file which I downloaded from its website. I can not change the installation directory in that case. I would like to know that is there a way to tackle this issue.
If you're using anaconda, your default environment is Python 2.7. You need to create a new environment and install matplotlib in there.
In a command prompt, do the following (saying yes to the questions):
conda create --name mpl33 python=3.3 matplotlib ipython-notebook
activate mpl33
ipython notebook
You should be able to import matplotlib when the notebook server comes up.
The first command simultaneously creates the environment and install
the listed packages.
The second command activates the new environment by prepending its location to the system path
The third command just starts the ipython notebook so that you can test out everything
I don't know how pycharm works, but my guess is that you'll have to tell it to look for the right python that you want to use. In this case it'll be something like: C:/Users//anaconda/envs/mpl33. In any case, the command prompt should display the path when you activate the environment.
Once you've activated your environment, you can install more packages like this:
conda install pandas=0.12
conda install pyodbc statsmodels
You can specific version numbers of packages like the first command or simply accept the latest available version (default)
Assuming you've already installed a 3.x python env in anaconda, this one line should do the trick:
conda install matplotlib -n name
where name is the name you previously gave to your python 3 anaconda env. If you're not sure of the name you gave it, it will be the name of a subdir in the Anaconda\envs directory.
Background: I recently went through the same trouble with matplotlib not getting installed by default by anaconda when I added a full python 3 env, even though it's meant to. The above line solved it for me; it gave me the following warnings so it seems likely that the two different available versions caused it to initially install neither. However it allowed me to choose the one I wanted, and then everything worked great.
Warning: 2 possible package resolutions:
[u'dateutil-2.1-py33_2.tar.bz2', u'matplotlib-1.3.1-np18py33_1.tar.bz2', u'numpy-1.8.0-py33_0.tar.bz2', u'pyparsing-2.0.1-py33_0.tar.bz2', u'pyside-1.2.1-py33_0.tar.bz2', u'python-3.3.5-0.tar.bz2', u'pytz-2013b-py33_0.tar.bz2', u'six-1.6.1-py33_0.tar.bz2']
[u'dateutil-2.1-py33_2.tar.bz2', u'matplotlib-1.3.1-np17py33_1.tar.bz2', u'numpy-1.7.1-py33_3.tar.bz2', u'pyparsing-1.5.6-py33_0.tar.bz2', u'pyside-1.2.1-py33_0.tar.bz2', u'python-3.3.5-0.tar.bz2', u'pytz-2013b-py33_0.tar.bz2', u'six-1.6.1-py33_0.tar.bz2'
]
conda install -c conda-forge matplotlib

Categories