0

So I have been fiddling around as well as conducting some serious work with Python for quite some time. Though, I still have some issues with it every once in a while.

I find it to be the most comfortable using PyCharm CE when working with Python. The typical scenario is that I just create a new virtualenv, launch PyCharm and open up my virtualenv there. And from there on out, it's like on auto-pilot, PyCharm handles all the dirty work related to which site-packages and Python runtime to use.

I always like to keep my virtualenvs clean and organized, so I often find myself semantically organizing my source code into submodules/subfolders. So whenever I want to import some code, or class, or whatever from another folder I just import it.

Imagine I have the following structure in my virtualenv:

├── bin
├── include    
├── lib           
└── src
    ├── foo.py 
├── important_scripts
    ├── some_script.py
    └── some_other_script.py
└── pip-selfcheck.json

Now, somewhere in foo.py, I want to use a function named A() that is implemented in some_script.py. The obvious way would be to add a simple line to foo.py - something like from some_script import A. Doing such works perfectly when I run and debug my code (foo.py in this case) from PyCharm.

As opposed to the the typical scenario I have described above, I wanted to do the same from the Terminal.app. So I fire up the Terminal, cd to my virtualenv and activate it. Then, what I do is, using the Python executable that is under the bin folder in my virtualenv, I try to run foo.py (At least this is what I think is the equivalent of right-clicking and running foo.py from the PyCharm window). Unfortunately, I get the error ModuleNotFoundError: No module named 'some_script'.

I think I am missing a simple detail or something. Because like I said, it works like magic when run from PyCharm.

Anyways, any advice or help will be highly appreciated. Thanks in advance.

Emre Akı
  • 135
  • 11
  • See [Import a file from a subdirectory?](https://stackoverflow.com/questions/1260792/import-a-file-from-a-subdirectory) – Galen Dec 11 '17 at 01:26

1 Answers1

0

Thanks for all the responses and references to possible solutions. While researching online, I have come across various instances of more or less the same problem that people were having while importing modules and packages. So, this is how I have just resolved it:

  • Under the important_scripts directory, I have included a file named __init__.py. This basically just tells the Python interpreter that this is indeed a Python package, rather than an ordinary subdirectory.
  • In this __init__.py, I have added in the line

    from important_scripts.some_script import A 
    
  • Then in the script, from which I will be importing the function A, that is, foo.py I have included the following lines:

    import os
    import sys
    sys.path.append(os.path.dirname(os.path.dirname(__file__)))
    

    which basically appends the virtualenv into the site-packages.

Emre Akı
  • 135
  • 11