Cannot run from pychon console in Pycharm but can run scrip - python

It is a wired question. I have a project folder and in this folder, I have a project.py and another src folder for my own module, let's say module.py. I also made this src folder as my root source folder.
The project.py can be run successfully(right-click the tab and select run project.py). However, when I copy the whole script into the python console and run it, it says ModuleNotFoundError: No module named 'module'.
Any thoughts?

Related

Using __file__ in script to get current file directory of script and executable file

I'm trying to write universal scripts that would work on any PC that meets requirements and I have a little problem with getting directories and paths right. Currently I have two folders in one location: C:\Users\myProject, one with my Scripts and other named Tests. In Scripts folder I store my python scripts with all instructions and in Tests I have .bat files used to execute scripts. To get parents path I decided to use (__file__), for example in my script - Test_Script.py:
__file__ = 'Test_Script.py'
print(r'__file__', __file__)
curr_path = os.path.dirname(os.path.abspath(__file__))
print (curr_path)
And it works, but in two ways. If I run cmd in folder with Scripts and type: py Test_Script.py, I get:
Test_Script.py
C:\Users\myProject\Scripts
But when I run it from Tests directory, I get:
Test_Script.py
C:\Windows\system32
How do I fix this to have same output as in first case while running script via cmd?

Seeing "ModuleNotFoundError: No module named ABC" in Visual Studio Code Python

This is my current project folder settings in VSCode:
main_folder
-folder1
__init__.py
main.py
-core
__init__.py
core.py
-checker
__init__.py
checker.py
-venv
The problem arises when I try to import a class from init.py in core_folder by doing this:
from core import MyClass
Funny thing here is that when I right click on VSCode and select Run Python File in Terminal, IT WORKS!
But when I select all the lines in my main.py and run Run Selection/Line in Python Terminal, I get ModuleNotFoundError: No module named core.
My understanding is that since folder1 has __init__.py, and a file from that folder is being executed, all the subfolders with __init__.py will be recognized as modules and I should be able to import from core. This works when running this file in the terminal but why not in the Selection/Line in Python Terminal?

Run Python module from command line like in Pycharm

I have a directory structure like this:
my_project
.idea
src
config.ini
method.py
... other py modules
When I open the project in PyCharm and run with the configuration below, it runs perfectly. It runs src.method as a module using an Anaconda 3 environment. The src.method module uses relative imports from other modules in src and takes config.ini as an argument.
I am trying to run the module in the same way through the Windows command line from src as the working directory
C:\Users\deimos\Anaconda3\envs\cveureka\python.exe -m src.method "config.ini"
but this gives a ModuleNotFoundErrorsaying it could not find src. I tried replacing src.method with just method but that gives ImportError: attempted relative import with no known parent package.
I have also tried setting the PYTHONPATH before running the module with
setlocal
set PYTHONPATH=%2
like in this answer, but to no effect.
It there a way to set up the command line to replicate the way that PyCharm runs the module?

Run python script in package

I am struggling with running python script in shell. I use PyCharm where is everything ok, but I want to run script without running PyCharm.
So my project folder is like:
data/
file.txt
main/
__init__.py
script.py
tools/
__init__.py
my_strings.py
I want to run main/script.py, which start with from tools import my_strings and working directory should be data/.
My PyCharm config is:
Script path: <PROJECT>/main/script.py
Working directory: <PROJECT>/data
Add content roots to PYTHONPATH: YES
Add source roots to PYTHONPATH: YES
So I want to run main/script.py in shell on Ubuntu. I tried:
PYTHONPATH=<PROJECT>
cd <PROJECT>/data
python3 ../main/script.py
But I just got: ImportError: No module named 'tools'
Check out this post, it's explains the PYTHONPATH variable.
How to use PYTHONPATH and the documentation the answer points to https://docs.python.org/2/using/cmdline.html#envvar-PYTHONPATH
When you run from the data directory by default python can't find your tools directory.
Also regarding your comment about needing to run from the data directory, you could just use open('../data/file.txt') if you decided to run from the main directory instead.
Ideally, you should be able to run your script from anywhere though. I find this snippet very useful os.path.dirname(sys.argv[0]). It returns the directory in which the script exists.
I simply forgot to export $PYTHONPATH as suggested by Steve.

Cannot import folder into project in Aptana

I want to import a python package foo into a new project in Aptana.
I tried two methods: 1. promote file folder to project and 2. import folder into a new project.
In both cases, the error: "no module named foo.filename" occurs when I try to run
a program in a subfolder. The parent folder is called foo. The code is:
from foo.filename import *
There are no reference errors when executing this file outside Aptana.
How can I fix this?
Make sure each folder in your package contains an:
__init__.py file.
If the package folder is not inside of the projects root source directory you may want to add it to the PYTHONPATH environment variable.

Categories