Pycharm Terminal does not seem to use virtual environment - python

I have a venv setup with Python 3.7 with several packages installed. I installed them using the UI in settings and the scripts run fine.
However, when I go to the Terminal window and try to run my script there there, it gives me an error message saying "No module named xxxx".
Running python3 run_glue.py, it curiously gives me the following error:
sh-3.2$ python3 run_glue.py
Traceback (most recent call last):
File "run_glue.py", line 27, in <module>
import torch
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/torch/__init__.py", line 79, in <module>
from torch._C import *
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/torch/_C.cpython-36m-darwin.so, 9): Library not loaded: /usr/local/opt/libomp/lib/libomp.dylib
Referenced from: /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/torch/lib/libshm.dylib
Reason: image not found
It seems that it is looking at a python 3.6 environment despite my virtual environment being 3.7. I checked my settings and I dont see why it would be looking at the python 3.6 environment.
Any thoughts here?

You need to activate your environment first:
sh-3.2$ source path_of_your_venv/bin/activate
You should now see an (env) before your bash prompt:
(env) sh-3.2$
Then you can check if python3 is correctly referencing the environment by which python3.
Alternatively, execute python3 directly from within your environment:
./path_of_your_venv/bin/python3 run_glue.py.

Related

Error while running Package in Command prompt , No module found

I have created a package named as patterns along with test.py,
test.py has the following code lines:
from patterns.shapes import *
square()
[Module Error]
I have copied the test.py file to the same location F:, where I trying to run via command prompt.
To gain access to this package from any location I have installed it using pip install . in the windows command prompt, which was successful, but when I try to access the file test.py I am getting the error as below :
F:\>py test.py
Traceback (most recent call last):
File "test.py", line 1, in <module>
from patterns.shapes import *
ModuleNotFoundError: No module named 'patterns'
My package folder is stored in a different drive.
While working on a python project, one must always work in a virtual environment. Please check if you have your virtual environment activated. If not, then follow the steps:
cd your_working_directory
virtualenv environment_name where environment_name can be any name you want.
environment_name/Scripts/activate if on Windows
Or environment_name/bin/activate if on MacOS
Then after activating the environment you can install all the dependencies using pip. i.e pip install patterns

python virtualenv from terminal not working but working from pycharm

Python3 showing error while trying to create a new virtual environment from terminal using
virtualenv name_goes_here
with error,
Traceback (most recent call last):
File "/home/w3e/.local/bin/virtualenv", line 7, in
from virtualenv import main
ImportError: cannot import name 'main' from 'virtualenv' (/usr/lib/python3/dist-packages/virtualenv/init.py)
But it works when I do it from PyCharm > Settings > Project Interpreter > Add Python Interpreter
See the attached image below-
The command you were using doesn't exist.
Try this instead:
python3 -m venv your_venv
And activate it using:
source your_venv/Scripts/activate

I can't import pygame inside pycharm

I can't import pygame inside pycharm.
I've been using Linux Mint 19, Anaconda 3, Pycharm 2019.3.1.
I've created a conda environment and install pygame: conda install pygame -c cogsci
Then I ran (my_env) tiago#tiago-XPS-8700:~$ python3 mycode.py. And I got the following error:
teste.py:3: RuntimeWarning: use mixer: libSDL_mixer-1.2.so.0: cannot
open shared object file: No such file or directory (ImportError:
libSDL_mixer-1.2.so.0: cannot open shared object file: No such file or
directory) pygame.mixer.music.load('bat.mp3') Traceback (most recent
call last): File "teste.py", line 3, in
pygame.mixer.music.load('bat.mp3') File "/home/tiago/anaconda3/envs/pycharm/lib/python3.5/site-packages/pygame/init.py",
line 105, in getattr
raise NotImplementedError(MissingPygameModule) NotImplementedError: mixer module not available (ImportError:
libSDL_mixer-1.2.so.0: cannot open shared object file: No such file or
directory)
After reading some blog at internet I installed sudo apt install libsdl-mixer1.2.
After that if I run (my_env) tiago#tiago-XPS-8700:~$ python3 mycode.py at Linux terminal at my conda environment, my code run successfully.
But If I try to run the same code inside Pycharm I get the following error:
/home/tiago/anaconda3/envs/pycharm/bin/python
/home/tiago/Documentos/PYTHON/cursoemvideo/exercicios/ex_021.py
Traceback (most recent call last): File
"/home/tiago/Documentos/PYTHON/cursoemvideo/exercicios/ex_021.py",
line 3, in
import pygame File "/home/tiago/anaconda3/envs/pycharm/lib/python3.5/site-packages/pygame/init.py",
line 133, in
from pygame.base import * ImportError: libSDL-1.2.so.0: cannot open shared object file: No such file or directory
Process finished with exit code 1
What am I missing?
In conda environment they have pre installed packages and libraries which u can easily use by importing them but in pycharm u additionally need to intall that package so that u can use it in pycharm
Inspired by this questions Tensorflow and Pycharm, I ran Pychram from the Linux Terminal at my conda environment and it worked! If I run Pycharm from Linux Mint Menu I still can't import pygame.
Addicionally, I inserted the following statement at my .bashrc so that I can run pycharm from anywhere in the Linux Terminal.
# Open bashrc to edit
nano .bashrc
# Add the comand line below to the end of file
# You should adapt to your path to "pycharm.sh', mine is "/opt/pycharm-community-anaconda-2019.1.3/bin". In another computer I found it at "/var/lib/flatpak/app/com.jetbrains.PyCharm-Community/current/active/files/pycharm/bin"
export PATH=$PATH:/opt/pycharm-community-anaconda-2019.1.3/bin
# Then close the terminal and reopen it in order to the modifications in .bashrc take effect
# At terminal you run
pycharm.sh

Python - import error when trying to activate a virtual environment, or lauch Spyder

I get an ImportError whenever I try to activate a virtual environment, or when I try to launch Spyder.
When trying to activate a virtual environment:
Traceback (most recent call last):
File "/home/pauline/anaconda3/bin/conda", line 3, in <module>
from conda.cli import main
ImportError: No module named conda.cli
When trying to open spyder:
Traceback (most recent call last):
File "/home/pauline/anaconda3/bin/spyder", line 2, in <module>
from spyderlib import start_app
ImportError: No module named spyderlib
I tried to find an answer for that but I could mainly find problems occurring after Anaconda was just installed (mine has been installed previously and was working fine up until yesterday).
I have also tried this answer and this answer but they did not solve the problem.
The only think I can think of which may have provoked this error is that I changed the interpreter used by Spyder yesterday from the default Anaconda Python interpreter to an interpreter from a virtual environment created with virtualenv. Even then, I could close and restart Spyder with no problems, and the errors started after I rebooted my computer.
[edit] I should add that both Anaconda and my virtual environment use the same version of Python which is Python 3.5
If you are using a different python version, whatever packages that you had with anaconda or that you may have installed with conda install will not be there on the new version. You need to install them with pip or conda again.

django wont show up in virtualenv

I am trying to use virtualenv.
I installed it on my ubuntu system followed simple steps given here
but when I activate my virtual environment and try to do
$ python manage.py runserver
it throws error
Traceback (most recent call last):
File "manage.py", line 8, in <module>
from django.core.management import execute_from_command_line
ImportError: No module named django.core.management
I tried to see if django is accessible from my virtual env so it tried to
$ python
>>> import django
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named django
and the same import django works fine out of the virtualenv.
I also tried to delete the django folder from root python setup and reinstalling it. but all in vain, the problem is still the same. What should I do now ?
PS: I tried to run easy_install Django & easy_install django gazillion times from the virtual env everytime it says django is already their.
PPS: I dont know if it is of importance but their was a django installation present prior doing all this in the system.
Check your manage.py, the first line needs to define the python executable, usually #!/usr/bin/env python. This should be the path to the python executable in your virtualenv and not to the global executable. If you had installed Django system wide this wouldn't be a problem.
Just in case
virtualenv venv
source venv/bin/activate
python manage.py runserver
[Adding this info for Windows users who hit the same problem]
If you're running into this problem on Windows then make sure that your virtual env is active (your prompt will be something like (venv) d:\myproject) and that you're running python.exe explicitly.
On the latter point, do not run py.exe (the Python Launcher introduced in 3.3) or rely on a .py file association to run Python. Both of those will run a version of Python outside of your virtual env and therefore will have the wrong site-packages.
If your virtual env has django 1.8.6 but your core Python installation does not have django, then:
python -c "import django;print(django.get_version())" will work and print 1.8.6
py -c "import django;print(django.get_version())" will fail with "ImportError: No module named django"

Categories