Error while running Package in Command prompt , No module found - python

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

Related

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

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.

Python Virtualenv : ImportError: No Module named Zroya

I was trying to work with python virtualenv on the Zroya python wrapper around win32 API. Although I did installed the modules using pip, and although they are shown in cli using the command
pip freeze
,when trying to execute the .py file that uses the modules it shows the following error.
Traceback (most recent call last):
File "TesT.PY", line 2, in <module>
from zroya import NotificationCenter
ImportError: No module named 'zroya'
What is the reason for this cause ? I'm using python 3.4. When checked on
>>>help("modules")
on python cli, the modules that were installed using pip aren't listed.
Your virtualEnv is separate from OS Python environment.
Check whether you installed 'zroya' module in OS Python env or virtualEnv.
Run bin/activate in your virtual env, and check if 'zroya' module exists
>>>help("modules")
If no module named Zroya, run install command pip install xxx after activating virtual env.
Installing zroya should solve your problem.
Installation instructions: https://pypi.python.org/pypi/zroya
There seems to be some issue with the pip install. Copying the zroya directory from github repository over to PYTHONPATH resolved the conflict.

Running `pip` in the Google Cloud Shell causes ImportError

When I create a new project in Google Cloud Console and just try to run
pip
I get this error:
Traceback (most recent call last):
File "/usr/local/bin/pip", line 5, in <module>
from pkg_resources import load_entry_point
File "/home/sguergachi/.local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 70, in <module>
import packaging.version
ImportError: No module named packaging.version
I'm not sure how to fix this. Reinstalling doesn't seem to be an option for google cloud as it would for local environment.
It seems to be able to do anything in pip on GCP you need to run as sudo.
sudo pip
This fixed my issue.
First, pip and python are for Python 2; pip3 and python3 are for Python 3. Prefer Python 3 as most apps are finally getting away from Python 2.
Install Python apps in your $HOME folder, which persists across your GCP time/space continuum:
pip3 install --user <app>
Note you'll want to add ~/.local/bin to your PATH environment variable, discussed at length in https://unix.stackexchange.com/a/26059/46798, by adding the following to ~/.profile:
# add local Python apps to PATH
if [ -d "$HOME/.local/bin" ] ; then
PATH="$HOME/.local/bin:$PATH"
fi

Why doesn't my installed (on Windows in a virtual environment) python script work without a full path?

I wrote a small python package and then installed it in my Python 2.7 virtual environment using pip. My package includes a command-line utility script, let's call it my_script.py that uses functionality from my package via an import mypkg call. The script is successfully installed into my virtualenv Scripts folder, but after activating my virtualenv, if I call
(my_env) > python my_script.py --help
I get the error:
python: can't open file 'my_script.py': [Errno 2] No such file or directory
and if I call
(my_env) > my_script.py --help
I get the error
Traceback (most recent call last):
File "C:\PyEnvs\my_env\Scripts\my_script.py", line 1, in <module>
import mypkg
ImportError: No module named mypkg
(even though mypkg clearly shows up in the pip list). Only if I call
python C:\PyEnvs\my_env\Scripts\my_script.py --help
does the script work as expected. Am I doing something wrong, or is this a deficiency of virtualenv on Windows?
(Further note that C:\PyEnvs\my_env\Scripts is in my %PATH%, and I get the same behavior even if C:\PyEnvs\my_env\Scripts is also in my %PYTHONPATH%.)
> python my_script.py
will only work if the current directory is in your PATH environment variable. If it is not, you must specify the directory it is un, either completely, or relative to your working directory.
so you could do:
> python .\myscript.py
or update your PATH and add "." for current directory.
http://en.wikipedia.org/wiki/PATH_(variable)

Categories