Python script stops working when run outside of pycharm - python

I wrote my first program with a non built-in module and it works when I run it in pycharm, but when I try to run it in cmd I get an error: app.py, line 1, in <module> from pytube import YouTube ModuleNotFoundError: No module named 'pytube'
I've tried installing pytube module via cmd once again, but the problem still occurs
EDIT: The rest of my scripts are working fine (there are using built-in only modules)

Have you tried installing Pytube with pip on your command prompt? Enter: pip3 install pytube in your terminal and rerun the script. It wasn't clear with the original post if you already did this.
If this doesn't work, try running your script with the -m flag: $ python -m main
A more detailed answer for this issue is already available here.

Did you create an environment when you were in pyCharm? There's a chance you have to activate the env when you are in the cmd prompt.
Console without an environment
C:\path>
Activate environment named 'base'
C:\path>activate base
Console with an environment named 'base'
(base) C:\path>

Ok, so after some research and trying different solutions, I managed to run my script just by copying the missing module to the same directory as my app.py file, I don't know if it's the best solution, but it works.

Related

Pipenv script won't run on Windows using Git Bash

I am running Gitbash for windows and have installed pipenv using pip. Yet when I invoke pipenv nothing happens:
Since there isn't a command not found error, I believe the script is recognized it just isn't running properly.
Pipenv is installed globally:
I also can confirm that the Scripts folder is in the file path:
I don't think it is a problem with Gitbash because I can run other pip packages in the same scripts folder:
I'm also able to run python pipenv.exe but not pipenv.exe when in the Scripts folder:
I've struggled with this myself untill just now.
I've got a few suggestions.
SOLUTION 1:
I was calling Poetry to try and make sense of it as well and then I tried calling both applications from Powershell. Both work as inteded.
SOLUTION 2:
Calling python -m pipenv, as sugested in this other Stack Overflow thread, also works as intended. You could alias the command to pipenv in git bash and call it a day too.
Quoting the docs on the -m flag:
When called with -m module-name, the given module is located on the Python module path and executed as a script.
Quoting appdividend.com:
The -m flag in Python searches the sys.path for the named module and executes its contents as the __main__ module.
It goes through whatever path is in which pipenv and executes the module as a script.
SOLUTION 3:
This is how I ended up fixing my Pipenv blunder.
Reinstall Python and all packages to AppData.
MINGW64 was having trouble seeing Pipenv in C:\Program Files. I also noticed that I had some packages in AppData\Roaming as well, so I figured I'd reinstall Python, unticking the Install for all users option (to trigger AppData installation) to see if I could wrangle all packages together.
After that I tried installing Pipenv and succeeded in calling it as expected.
This is all highly anecdotal. I have no idea why MINGW64 failed to call JUST Pipenv and not Poetry, but this is how I've fixed it on my end.

"ModuleNotFoundError: No module named 'kivymd'" in .spec file

I already did pip install kivymd in my Python project. I also had the kivymd directory in my project.
I'm working with a Mac.
I created a spec file called "coinsnack4.spec" including the code below:
from kivymd import hooks_path as kivymd_hooks_path
However, when I try to package my python project with the spec file with the command:
pyinstaller -y --clean --windowed coinsnack4.spec
I got the error below:
File "coinsnack4.spec", line 3, in <module>
from kivymd import hooks_path as kivymd_hooks_path
ModuleNotFoundError: No module named 'kivymd'
I really don't know why this happens because I already pip install kivymd. I don't know what to do next and I would appreciate if anyone could help me with this error.
Thank you very much!
Why are you facing this issue?
The reason behind this is the concept of virtual environments in python. Each virtual environment is independent of the other. You can use different virtual environments, activate and deactivate them as per your project's requirements.
I would suggest you go through this doc once Python venv
As when you do a `pip install <SOME_PACKAGE> from your local terminal, it installs the package from into the default python environment and from the terminal itself (not pycharm terminal) if you try to execute the python program it will work fine but as soon as you switch to pycharm or any other IDE, it has it's own python environment set and that environment is unaware of what happened in the other python environment. So you need to install the pip package here also, in order to execute the same python program.
Solution:-
The first thing I would suggest is to install the package in the virtual environment that the pycharm is using. For that click on the Terminal icon the below bar of your pycharm window. then do run the below command :-
python3 -m pip install kivymd
If this doesn't work, try configuring the python environment in pycharm.
Below is how you can change or update your python interpreter in pycharm: -
Setting an existing Python interpreter
At any time, you can switch your Python interpreter either using the Python Interpreter selector or in the Project Settings/Preferences.
Creating a new Python interpreter
To add a new interpreter to the current project:
If you have a conda environment, follow the below steps: -
Or if you want to setup a new virtual environment, do as below: -
I think you installed pyinstaller not in project's virtualenv, just:
pip install pyinstaller
then problem will be fixed.

How to properly install a module in python?

I 'm new to python, and I need some help.
At first i install pyperclip using command window: "pip3 install pyperclip". The command window responsed that I have downloaded the module successfully.
However I can not use pyperclip in my code. When I coded "import pyperclip", there was an error: "No module name pyperclip..."
I then changed the directory of the command window direct to the address of Python, but nothing seem to work.
I use PyCharm for my coding.
Hope you can hep me solve this problem.
Python responsed "No module name pyperclip..."
It clearly that I have download the module.
Find path to python which you are using and then do something like:
path/to/python -m pip install pyperclip
Or better use conda or pipenv
The problem is that you have installed the package for another interpreter, one that is not used by your current project.
How to install modules for a particular interpreter in PyCharm: https://www.jetbrains.com/help/pycharm/installing-uninstalling-and-upgrading-packages.html
You can also select the interpreter for the particular configuration (setup used for running a script):
https://www.jetbrains.com/help/pycharm/creating-and-editing-run-debug-configurations.html

Flask is not importing even though I installed it

I'm on Windows 10 and I am using PyCharm to run my application. I'm pretty new to Python and just coding in general.
I typed:
pip install flask
in the cmd prompt but forgot to open cmd prompt under administrator privileges. Could this be the problem? I ended up re-opening the cmd prompt with administrator rights and typed
pip install flask
again and it showed that it was already installed. I'm pretty sure I have flask installed on my system.
When I try to run the python file below which I named test_webapp.py I get the following error:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run()
I get an error:
ModuleNotFoundError: No module named 'flask'
when i type in python --version into the command prompt i get version 3.6.3.
When i type the same code into the Python 3.6 (32-bit) command line, the code works! However, when i try to run it using the IDLE I get a SyntaxError and the number 6 in Python 3.6.3 gets highlighted in red.
Any ideas as to what's going on?
Try to install flask with specific version of python, do something like:
python[version] -m pip install flask
Note: replace [version] with python version (such as python3).
Sounds like the pip you are using is not installing for the same python you are using.
try which pip and which python
the they should be in the same folder. Might want to make an alias or two if you have a bunch of pythons (sys, 2, 3, conda etc) or just start with a fresh virtual env for your project. <- best imo.
When you typed pip install flask in the command prompt, it installed flask in the global environment. That is the reason when you run it in command line it works because it used global environment by default.
Pycharm has a virtual environment, therefore you need to install it in that virtual environment. Open Pycharm, then open the terminal within Pycharm and type "pip install flask".
You can read about virtual environment in python to get a clear idea.
Check version of python
python --version
And then use accordingly
if python 3.x.x then type
python3 app.py
if python 2.x.x then type
python2 app.py

pyInstaller: ImportError: No module named 'praw'

I wanted to pack my script using pyInstaller. I run pyinstaller file.py -F, file is created successfully, but when running I get ImportError: No module named 'praw'. So I created new file containing only import praw and run pyinstaller file.py -F --hidden-import=praw but still get the same error when running.
I was unable to find anything similar, most issues were solved by using --hidden-import.
Any ideas on how it can be solved?
EDIT:
praw is installed inside virtual environment and running the script directly works as expected.
Seem pyinstaller run outside the virtualenv.
Try switch to your virtualenv and run:
python -m PyInstaller -F file.py
I'll recommend to look at pyenv or virtualenv. Activate these env's and install the praw module here. This should work.
This command might help you out. It installs the Praw module for you. Make sure you have pip installed!
pip install praw
I found a way to solve the issue:
When using Python2.7, or starting the shell like python2, we need to do
python2 -m pip install --user praw
to make sure they are linked during installation.
Same idea for python3 shell.

Categories

Resources