python virtualenv from terminal not working but working from pycharm - python

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

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

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

ModuleNotFoundError When executing Python command inside bash script

The script runs fine from pycharm but can't see my my lib
-> site-packages when ran as a shell script. I've set my PYTHONPATH variable and added it to path as %PYTHONPATH% but no luck. I've checked bashrc and can't see the environment variable being overwritten. Maybe my structure is incorrect?
-lib
- site-packages
-scripts
-src
- monroes_events.py
**Error:**
(venv) C:\Users\James\repos\web_scraper\src>bash web_scraper_setup.sh
python3 Monroes_Scraper.py
Traceback (most recent call last):
File "Monroes_Scraper.py", line 5, in <module>
import pandas as pd
ModuleNotFoundError: No module named 'pandas
Install pandas module using pip install pandas which is required to run the Monroes_Scraper.py
You can find more detail here

Pycharm Terminal does not seem to use virtual environment

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.

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