Python module visiable only in iPython - python

I have used set PYTHONPATH=%PYTHONPATH%;dictionary_containing_modules to make some modules globaly visiable.
I have a script importing the modules with import my_module.
When i run the script from the iPython shell (run my_script.py), i get no errors and the script runs as intended, but when I run the script from the command promt (windows) with python my_script.py I get the error:
ImportError: No module named my_module
I checked with pwd that they use the same working directory.

Remember that you can dynamically change your system path from within your script, using sys.path or the site module. So maybe you want to add them to your script...
Or maybe you want to write a BAT or Python launcher script that sets the PYTHONPATH...
Or you want to edit the Windows environment variables (somewhere inside System properties Win+Break).

Related

ModuleNotFoundError: No module named 'seaborn'. How to redirect path in Python to capture libraries?

on a Mac, I have pip3'd installed a few libraries in python. When I try to 'import', for example, seaborn, I receive a "module not found error." How can connect to the proper path? What is the "PATH" referred to in screenshot?
the issues appears to be synching library with proper path. Already installed libraries, but they will NOT import under python import command, says the following
PATH is the environment variable on your system where all programs and scripts are located, which can be directly executed from the terminal/cmd/shell.
If you had a program called myProgram.exe in C:\Users\Me\Desktop\, you could add C:\Users\Me\Desktop\ to the PATH variable and execute the program using
myProgram
in the terminal. Otherwise, you would have to include the path like:
C:\Users\Me\Desktop\myProgram.exe
You can edit this variable like described with nice pictures here
To execute python and pip from the console, you should have C:\Users\...\AppData\Local\Programs\Python\Python37 (or wherever you installed Python to) in your path variable :)

Python Modules and submodules in ROS catkin workspace

I am trying to run python code in ROS and it includes a module (module not made for ROS) that I had to pip install. When I try running the code in my catkin workspace with roscore running, it cannot find the module I installed and gives me an ImportError. Any idea on how to run already created modules on ROS with rospy?
Making sure you can find the module
Here are 3 options. They all should work and are in order of hackiness.
You can add the module to a package.xml like in this ros numpy tutorial.
<build_depend>python-numpy</build_depend>
<run_depend>python-numpy</run_depend>
Set your $PTYHONPATH environment variable in your .bashrc:
export PYTHONPATH=$PYTHONPATH:/path/to/your/package_or_module
Inject a path to the module before importing it in your python code:
import sys
sys.path.insert(0, "/path/to/your/package_or_module")
Making sure you're running the program correctly
Resource your .bashrc and catkin workspace before running the program.
If you are trying to use rosrun package_name filename.py then you need to make sure the file is an executable:
chmod +x pythonfile.py
and the top of the python file should have the code:
#!/usr/bin/env python
Consider using python directly to run the file like akshayk07 mentioned
python filename.py

Fail to import tensorflow from a program called within a script via ssh [duplicate]

When I try to run my program from the PyCharm IDE everything works fine but if I type in Fedora:
python myScript.py
in a shell prompt I get an import error from 1 of the module.
ImportError : No modue named myDependency
What does PyCharm do that allows the interpreter to find my dependencies when launched from the IDE? How can I get my script to find its dependencies so it can be launched with a singe command?
There are a few possible things that can be causing this:
The same python interpreter? Check with import sys; print(sys.executable)
Is it the same working directory? Check with import os; print(os.getcwd())
Discrepancies in sys.path, which is the list python searches sequentially for import locations, can possibly caused by environment variables. Check with import sys; print(sys.path).
Adding this worked for me:
from os import sys, path
sys.path.append(path.dirname(path.dirname(path.abspath(__file__))))
As a first step, in PyCharm go to project settings / python interpreter, and note the path. Then compare that to the result of which python -- do they line up? If not, you need to make them do so.
If that's ok, check what PyCharm defines as your project root in project settings / project structure. Is that the directory where your script is located? If not, you should run the script from that directory or append the directory to the $PYTHONPATH variable.
Almost definitely, it's one of those two things.
You might have set some project dependency in Pycharm for module myDependency.
You can access the same in Fedora by importing the module explicitly or by creating the egg of that module and installing it.
This will then go to python site-packages from where you can refer this dependency.

Running a python program in Windows?

I have Python 3.6 and Windows 7.
I am able to successfully start the python interpreter in interactive mode, which I have confirmed by going to cmd, and typing in python, so my computer knows how to find the interpreter.
I am confused however, as to how to access files from the interpreter. For example, I have a file called test.py (yes, I made sure the correct file extension was used).
However, I do not know how to access test.py from the interpreter. Let us say for the sake of argument that the test.py file has been stored in C:\ How then would I access test.py from the interpreter?
The simplest way would be to just do the following in cmd:
C:\path\to\file\test.py
Windows recognizes the file extension and runs it with Python.
Or you can change the directory to where the Python program/script is by using the cd command in the command prompt:
cd C:\path\to\file
Start Python in the terminal and import the script using the import function:
import test
You do not have to specify the .py file extension. The script will only run once per process so you'll need to use the reload function to run it after it's first import.
You can make python run the script from a specific directory:
python C:\path\to\file\test.py
In command prompt you need to navigate to the file location. In your case it is in C:\ drive, so type:
cd C:\
and then proceed to run your program:
python test.py
or you could do it in one line:
python C:\test.py
I may be misunderstanding what you are seeking, but I think what you are asking for is a IDE-like environment where you can load Python scripts, edit, and debug.
You already have IDLE, https://docs.python.org/3.6/library/idle.html, which came with the Python installation.
There are many IDEs available for Python. https://wiki.python.org/moin/IntegratedDevelopmentEnvironments Personally, I like using PyDev on Eclipse.

How do I run the python files from command shell/command prompt?

Really frustrated with this as one thing works at one time. Sometimes import filename.py works. But in the tutorials all I see is python filename.py. But when I try to give that, I am facing an error like invalid syntax.
I have edited all the environment variables and I have C:\Python27 folder in the same location. To be able to run the files using python filename.py what are the conditions that must be met? What should be the current working directory? Should the .py files be there in the same working directory?
import name is a python keyword for use within a python script and/or the python interactive interpreter (repl).
python filename.py is a shell command for use at a command prompt or within a shell script to run the python interpreter on a given python script file.
The working directory does not matter other than for whether the file listed in python filename.py can be found.
So for python filename.py to work you must be in the same directory as filename.py but you could just as readily use python c:\users\user\some\other\path\filename.py in which case the current directory isn't used to find the file.
If you get python syntax errors from attempting to run python on a python file that's a python error in the code and you will need to look at the python file to see what the error is.
Just to be clear, typing python filename.py only works from the Terminal (i.e. cmd.exe, Windows PowerShell, the "Terminal" application on a Linux kernel, etc.), not from the Python interpreter (i.e. python.exe), and only works if you have used the cd command to change into the directory in which the file is saved (so that the terminal knows where to look for filename.py). import filename can be used from the Python interpreter, but is not the ideal method as it creates a compiled version of filename.py and can only be used once (you would have to restart the interpreter to do so again). I'm not sure whether this works in the official Python distribution available from the Python website, but at least in the Anaconda distribution, you can run a file from the Python interpreter using runfile("C:/Users/CurrentUser/Subfolder/filename.py").

Categories