Modules working in Pycharm but not Shell or Command Line - python

I'm new to Python and having trouble with several modules. In Pycharm, everything works fine. I am able to import modules, pip installer works fine. However, for some reason I'm running into issues when trying to import modules in the Python Shell, or when running programs using the command line.
For example, I can import Beautiful soup just fine in pycharm, but when I try in the shell I get:
Traceback (most recent call last):
File "", line 1, in
import bs4
ModuleNotFoundError: No module named 'bs4'
Similarly, when I save a program in pycharm and try to run it from the command line, I get:
C:\Users\Tony>hellowness.py
Traceback (most recent call last):
File "C:\Users\Tony\MyPythonScripts\Hellowness.py", line 3, in
import selenium, bs4, pyperclip
ModuleNotFoundError: No module named 'bs4'
The correct directory is being used, because I can run simple programs that don't use modules without problems in the command line.
Sorry if this has been answered eleswhere. I searched the forum but people seem to being having trouble installing modules. The modules are installed fine (I think) they just don't work in the Python Shell or the command line. How do I go about troubleshooting this?
Thank you,
Tony

I finally figured it out. As you said there were two "Site Packages" directories installed. One for Pycharm and one for Python itself. I am sure there's a more efficient way to solve it, but for now I just copied any relevant modules from Pycharm's site packages folder over to Python's.
Thanks!

Related

PyCharm Doesn't Recognize Installed Package

I'm a beginner level Python user. Up until today I was able to use installed packages(pandas, numpy and so on) at PyCharm. I try to download BeautifulSoup and error msg took me to PyCharm web site. I followed instruction on my regular MacBook terminal (macOS Monterey 12.4) where it told me to upgrade the pip as pip3 and try the pip3 install beautifulsoup. After this code I get this from terminal Requirement already satisfied.
However since that update on terminal none of the packages on my PyCharm are working. PyCharm marks it with red underline, I click and choose install again but still not working. This is what I get
Traceback (most recent call last):
File "/Users/ezgiwon/PycharmProjects/pythonProject1/main.py", line 1, in <module>
from bs4 import beautifulsoup4
ModuleNotFoundError: No module named 'bs4'
Also for the rest it's similar to this error couldn't find __innit__.py
I've read so many solutions here but some didn't work some I wasn't able to understand with my current knowledge.
I tried the 'edit configurations', 'edit python interpreter',also tried the built-in PyCharm terminal to upload those packages. I'm not sure how to fix this issue. I thought I could delete everything following a tutorial and start fresh but seems like I didn't clear all past files.
Now I have a new file under Library/Framework for Python which I will be asking in separate question.
Please try to guide me as detailed as possible, much of these terms are new to me and really struggling to run PyCharm correctly. I really need it to run for simple study tasks as I was using it for very basic pandas and numpy calculations.
[interpreter][1]
[Paths][2]
[interpreter2][3]
Run in terminal:
pip install beautifulsoup4
Then import like this
from bs4 import BeautifulSoup

How to get packages to import in code within anaconda virtualenv?

Currently I cannot import package installed in virtual environment:
Traceback (most recent call last):
File "/Users/glada/GIT_REPOS/autoflux/main.py", line 5, in
import cx_oracle
ModuleNotFoundError: No module named 'cx_oracle'
Imports in python are sometimes a bit tricky because it depends on how you call a line of code. Do you call the import from the python REPL input?
>>> import cx_oracle
Do you call the line of code from inside a script?
$ python myscript.py
Do you run the script as a module?
$ python -m myscript
Also a folder structure would be helpful. Do you use virtualenv? Maybe you have installed the package to the wrong location (installed in main python installation site-packages and now you are trying to import inside an activated virtualenv)?
There is a brilliant answer on a similar question, which is really descriptive and once I also found very useful to understand imports in python. Maybe this helps you.

Using a Python Package Outside of Anaconda

I wish to run a package, openquake; however, it was installed separately from Anaconda, which I am using to run python.
[centos modules]$ which python
~/anaconda2/bin/python
I have set openquake up and I believe it's installed correctly, however, when I try to run my script, I receive the following error:
[centos maxdist]$ python ../script.py
Traceback (most recent call last):
File "script.py", line 12, in <module>
from openquake.hazardlib.source import ComplexFaultSource
ImportError: No module named openquake.hazardlib.source
I was wondering if I need to add a line to my script for it to find the openquake module for it to run successfully? In general, what's the best way to run a python package that is installed outside of the main Anaconda python instance? I am trying to avoid having to re-install openquake for Anaconda, as I was having a little bit of difficulty with that work-around.
Thank you.
If both environments, anaconda and default python that you installed the library for have the same python version, then
import sys
sys.path.append(r'/path/to/package')
from openquake.hazardlib.source import ComplexFaultSource
Check if this works. If the package is compiled for different version, then there will be an error. Also, I would suggest you to use one environment and avoid doing this for the sake of simplicity.

ImportError: No module named twisted.persisted.styles

From IDLE, I tried to run a script with a newly installed scrapy 1.0.3.
I'm using a script from a friend whom it worked for (but on Windows, I'm on a Mac).
From the import of scrapy on the first line, I get this error when running the program:
ImportError: No module named twisted.persisted.styles
The whole script, if it's helpful, points to this:
Traceback (most recent call last):
File "/Users/eliasfong/tutorial/tutorial/spiders/medspider.py", line 1, in <module>
import scrapy
File "/Library/Python/2.7/site-packages/scrapy/__init__.py", line 27, in <module>
from . import _monkeypatches
File "/Library/Python/2.7/site-packages/scrapy/_monkeypatches.py", line 20, in <module>
import twisted.persisted.styles # NOQA
ImportError: No module named twisted.persisted.styles
Any suggestions on how to tackle this problem?
Just try to force the update of twisted :
pip install twisted --upgrade
That works for me with python3.4 and Scrapy==1.1.0rc1
Either twisted is installed on your mac (I highly doubt it since it's not a standard library) and for whatever reason the IDE (i'm assuming that's what you mean since you typed "idle") or the terminal you are in doesn't have your updated environment variables, meaning it doesn't understand where your default python libraries are (again I highly doubt it), or you simple do not have twisted installed on your mac. If it's not installed you have a couple of options:
The easiest way to install a python package is through pip.
If that not an option you can try homebrew which is another package manager for macs. It offers an easy way to install packages correctly.
If that still is not an option for you or you simply don't want to attempt that you can download twisted directly from here (the .bz2 since you're on a mac), click on it and it should unzip it for you. Then just run setup.py and it should install it in the correct location on your mac.
If that still doesn't work and you have decent knowledge of unix. Use the "locate" command on the terminal and find out where your dist-packages directory is and put the source for twisted in there directly and then attempt to import twisted in your IDE or in the python interpreter to verify that it is installed.
note: If you're still having problems after it is installed trying restarting your IDE or messing with some setting to make sure your IDE has the right environment and python path. Hope that helps!
It could be related to having installed Python without bzip2. I had the same error and this helped me, see the accepted answer here:
Installing Twisted through pip broken on one server
Had this exact thing on FreeBSD. Solution (as root/sudo):
chmod -R go+rX /usr/local/lib/python2.7/site-packages
Some directory permissions weren't set up right on install.

Install Panda3d to run with python

I am running Ubuntu 10.04, I have python installed and running fine. When I installed pand3d from the deb package from the site and tried to run an sample. Like it is describe in this page:
http://www.panda3d.org/manual/index.php/Installing_Panda3D_in_Linux
I got the error:
Traceback (most recent call last):
File "Tut-Asteroids.py", line 13, in
import direct.directbase.DirectStart
ImportError: No module named direct.directbase.DirectStart
In the same page as above there is a description to how to solve this error. But I don't understand what do I need to do.
Can any one tell-me what do I need to do?
Found the answer. I had two pythons installed. One in /usr/bin and the other in /usr/local/bin. Turn out I needed to use the /usr/bin version to run what I needed. Hope this helps other!

Categories