Import statement works on PyCharm but not from terminal
You are running foo.py like a script, but you are really using it like a module. So the proper solution is to run it as a module:
python3 -m somepackage.foo
For the record, another alternative is to edit your path like:
export PYTHONPATH=.
(Or you could put the absolute directory in there, and of course you should append any other directories that are already in your PYTHONPATH.) This is closer to what PyCharm does, but is less philosophically correct.
Python script works in PyCharm but not in terminal
Edit: The original answer was based on the assumption that the script you're running is within the folder structure given, which a re-read tells me may not be true. The general solution is to do
sys.path.append('path_to_Assets')
but read below for more detail.
Original answer
The paths that modules can be loaded from will differ between the two methods of running the script.
If you add
import sys
print(sys.path)
to the top of your script before the imports you should be able to see the difference.
When you run it yourself the first entry will be the location of the script itself, followed by various system/environment paths. When you run it in PyCharm you will see the same first entry, followed by an entry for the top level of the project. This is how it finds the modules when run from PyCharm. This behaviour is controlled by the "Add content roots to PYTHONPATH" option in the run configuration.
Adding this path programmatically in a way that will work in all situations isn't trivial because, unlike PyCharm, your script doesn't have a concept of where the top level should be. BUT if you know you'll be running the script from the top level, i.e. your working directory will be the folder containing Assets and you're running something like python Assets/main.py
then you can do
sys.path.append(os.path.abspath('.'))
and that will add the correct folder to the path.
Script running in PyCharm but not from the command line
There are a few possible things that can be causing this:
- The same python interpreter? Check with
import sys; print(sys.executable)
- Is it the same working directory? Check with
import os; print(os.getcwd())
- Discrepancies in
sys.path
, which is the list python searches sequentially for import locations, can possibly caused by environment variables. Check withimport sys; print(sys.path)
.
My main.py script is running in pycharm IDE but not from terminal. Why is this so?
Looks like you are using venv,
did you activate it before running your script?
For Linux/Mac you can do the following:
. venv/bin/activate
For windows you should use:
source venv/Scripts/activate
after activating the virtual environment, you can install packages with:
pip install -r requirements.txt
Not able to run in PyCharm but able to run in terminal
The nltk module is installed globally and the python project has no access to it. Changing that setting will solve the problem :)
ImportError in console but not in PyCharm
The issue you are encountering relates to the way you run scripts in PyCharm - for each of the run configurations you specify the working directory.
The behaviour should be the same as long as you cd to the same working directory when running a script from the command line.
If you are unsure about which one is it you can check in the configuration or the console output.
It may also worth reading answers to this question: Import statement works on PyCharm but not from terminal
Import from my package recognized by PyCharm but not by command line
PyCharm sets the Python Path at the root of the project (by default). To mimic this in a quick'n'dirty fashion, you just need to do this once in your shell session before invoking python whatever
:
set PYTHONPATH=C:\Users\usr\PycharmProjects\project
Related Topics
Python - How to Separate Paragraphs from Text
What Is the Correct Way to Make My Pyqt Application Quit When Killed from the Console (Ctrl-C)
Pandas: Update Column Values from Another Column If Criteria
How to Change the Foreground or Background Colour of a Tkinter Button on MAC Os X
Cv2 Image Error: Error: (-215:Assertion Failed) !Ssize.Empty() in Function 'Cv::Resize'
Google Chrome Closes Immediately After Being Launched With Selenium
Python-3: Why This Following Code Returns None in Print Statement
Python Check Multi-Level Dict Key Existence
Convert Image Files to a CSV File
Stored Procedures With Sqlalchemy
How to Create an Automatically Updating Gui Using Tkinter
Counting the Number of Duplicates in a List
What Is the Most Pythonic Way to Check If Multiple Variables Are Not None
Conditional Row Read of CSV in Pandas
Replacing Values in a Dataframe for Given Indices
Convert String to Python Class Object
Selenium Python Send_Key Error: List Object Has No Attribute