Python works in PyCharm but not from terminal

2019-04-04 10: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

7条回答
Lonely孤独者°
2楼-- · 2019-04-04 11:03

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.

查看更多
干净又极端
3楼-- · 2019-04-04 11:06

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.

Hope this helps..

查看更多
该账号已被封号
4楼-- · 2019-04-04 11:06

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

查看更多
贼婆χ
5楼-- · 2019-04-04 11:14
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.

查看更多
我欲成王,谁敢阻挡
6楼-- · 2019-04-04 11:20

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.

查看更多
祖国的老花朵
7楼-- · 2019-04-04 11:25

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

查看更多
登录 后发表回答