Python works in PyCharm but not from terminal

12,770

Solution 1

sys.path.append(os.getcwd()[:os.getcwd().index('Dev')])

I added this to my imports and it seems to have solved the problem. However, this doesn't seem like it would be the right way to do it; it will do for now.

Solution 2

I too have had this issue - and the PYTHONPATH setting set by PyCharm did seem to be the issue.

My alternative (as I was nearly finished writing the code) was to generate a setup.py - and install the classes/structure in my local virtual Python environment.

Solution 3

When running a script from within PyCharm, it runs it in an environment with PYTHONPATH set to the list of all the folders that are marked "Sources Root" (with a blue folder icon) in the project explorer.

Outside of PyCharm, PYTHONPATH is not normally set. The first entry in sys.path refers to the current working directory where the script was run from. As long as you run your script with your terminal's working directory as the folder containing Dev, it should be able to find the Dev.test module, regardless of the extra entry added to sys.path.

Once you get the working directory correct, you should be able to remove the sys.path hack.

Solution 4

What @codewarrior has said about the PyCharm setting its own PYTHONPATH is correct. But sys.path didn't have my current working directory. So to get around this problem, I updated my PYTHONPATH (or you can edit sys.path).

Setting PYTHONPATH

export PYTHONPATH=$PYTHONPATH:`pwd`  (OR your project root directory)

Updating sys.path

import sys
sys.path.insert(0,'<project directory>') OR
sys.path.append('<project directory>')

You can use insert/append based on the order in which you want your project to be searched.

HTH.

Solution 5

Pycharm uses a virtual environment. When you try run your program in your terminal this enviroment isn't active.
You need to build or upload your enviroment Pycharm with your libraries. cd to the directory project and write in terminal:

source venv/bin/activate
Share:
12,770
user3591079
Author by

user3591079

Updated on July 10, 2022

Comments

  • user3591079
    user3591079 almost 2 years

    I recently figured out how to import modules for unittesting in python. As a solution to this, I use:

    sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
    from Dev.test import someclass
    

    This works fine while running in PyCharm and I get the expected output. However, when I run from terminal I run into an error:

    ImportError: No module named Dev.test
    

    I have the init files where they are supposed to be but I'm lost as to why this is working in PyCharm but not from the terminal. I have not changed my path or anything in PyCharm as this code is supposed to be able to run with minimal modifications on other machines. Any idea as to why this is happening and what I might be able to do to fix it?

    My folder structure is as follows

    -Current
    -Dev
     -__init__.py
     -test
      - __init__.py
      -someclass.py
      -Tests
       -__init__.py
       -someunittest.py
    

    I have tried running someunittest from the main folder as well as with a complete path but it only works in PyCharm