21

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

user3591079
  • 283
  • 1
  • 2
  • 10
  • Yes but after looking in terminal it is using 2.7, same as in PyCharm – user3591079 Jul 10 '15 at 19:21
  • 2
    What's your folder structure? Which commands are you using to test in Python and in Pycharm, and from what directory? – Alyssa Haroldsen Jul 10 '15 at 19:22
  • I added my folder structure for clarification. I'm sure that PyCharm is using the correct version but maybe it is my path although I have not changed that from the default. – user3591079 Jul 10 '15 at 19:27
  • Are those `__init__` or `__init__.py`? – Alyssa Haroldsen Jul 10 '15 at 19:27
  • __init__.py I should have included that – user3591079 Jul 10 '15 at 19:29
  • Are you running the python from the Current directory – The6thSense Jul 10 '15 at 19:31
  • I have tried both with the full path and changing the current directory to the top folder as well as in Tests – user3591079 Jul 10 '15 at 19:32
  • 1
    "I have not changed my path or anything in PyCharm" or `sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))` Which is it? – msw Jul 10 '15 at 19:54
  • That is included in the script. I have not changed anything in PyCharm itself. To my understanding, that line would run even when I ran it through terminal. – user3591079 Jul 10 '15 at 20:00
  • I feel I should be able to open arbitrary files with the charm command, but I can't either. It pisses me off and I can't find a solution here. So, here's to waiting for an answer... shouldn't the PyCharm team answer? – rjurney Oct 24 '16 at 01:26

8 Answers8

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.

user3591079
  • 283
  • 1
  • 2
  • 10
  • Hi @user3591079 . I seem to be having a similar problem, but can't seem to fix it. What do you mean when you say "I added this to my imports and it seems to have solved the problem." I tried doing this in my python script, but it still didn't work. Thanks! – hoof_hearted Jul 13 '15 at 16:36
  • For me this results in an error like `ValueError: substring not found`. Can you offer any guidance? – alex Oct 11 '17 at 12:26
1

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.

codewarrior
  • 2,000
  • 14
  • 14
  • 3
    But then again it does not for some strange reason. This worked for me in one project, but not in another. So there seems to be something else at work as well. – Zelphir Kaltstahl Aug 04 '15 at 09:45
1

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.

bad_coder
  • 11,289
  • 20
  • 44
  • 72
Tim Seed
  • 5,119
  • 2
  • 30
  • 26
  • Can you elaborate on this? What does `setup.py` do, exactly? What do you mean by "install the classes/structure in my local virtual python environment"? – alex Oct 11 '17 at 12:24
1

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.

avp
  • 2,892
  • 28
  • 34
1

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
Shamis
  • 2,544
  • 10
  • 16
AlitoxSB
  • 11
  • 1
0

I would recommend trying out $ pip install . in your source directory. This will install your own packages for your project.

0

To add to similar answers here, PyCharm is doing some extra config for you before running your script. If adding your sources root to PYTHONPATH doesn't work then examine your run configuration in PyCharm for the script in question, there will likely be some more behind the scenes magic at play.

GuessWork
  • 1
  • 1
-1

I had similar problem. I think the problem is that Pycharm modifies PYTHONPATH so before running your script:

  1. cd to the file where python file resides
  2. run export PYTHONPATH=.
  3. run the script

You can also create "main" python file where you set the python path and then call the other modules

mark
  • 354
  • 2
  • 5
  • 15
  • Not only does this not work for me, but on Git Bash for Windows 7, `echo $PYTHONPATH` returns `.` - not an actual path. – alex Oct 11 '17 at 12:22
  • @alex: Maybe this will help more: after imports in your script put this command: os.environ["PYTHONPATH"] = "." and rerun it. Please let me know is it works. – mark Oct 11 '17 at 12:31
  • @alex did it work? (putting os.environ["PYTHONPATH"] = "." right after the imports in a script) – mark Oct 12 '17 at 11:44
  • Nope. There's an issue with the first import, so the script bombs out before it reaches `os.environ` statement. If I put that line of code *above* the imports - `print os.environ["PYTHONPATH"]` still returns `.`. – alex Oct 12 '17 at 11:50