No module named Julia - python-3.x

I'm a new beginner with python und have recently installed Julia. And as I wrote "from julia import Main" in Jupyter note through Anaconda, an error would occur which is "no module named 'julia'". I don't know which part is wrong or is this about the path of the installation of Julia? enter image description here
I would appreciate it so much if you could help me.

"from x import y" is the syntax for importing Python packages in a Python script or notebook, but as you probably know, Julia is a separate language of its own, not a Python package.
If you want to use Julia in a Jupyter notebook, you will instead generally want to change the whole Jupyter kernel to Julia from Python, rather than just adding a package (though see comment from Przemyslaw above if you want to call Julia from Python for any reason).
The easiest way to install a Julia kernel for Jupyter and start a notebook using that kernel is to just start the Jupyter notebook from Julia. In other words, first start Julia, so that you have a Julia REPL that looks something like the image below, and then type
julia> using Pkg; Pkg.add("IJulia") # If IJulia is not already installed
julia> using IJulia
julia> notebook()
which will open up a Jupyter instance with a Julia kernel installed, from which you can open a new notebook using that Julia kernel
which should look like the following (note the Julia symbol in the upper right corner)
Note that at no point in this process do you use Python or Anaconda for anything, and note also that in Julia there is no from keyword and also generally no such thing as Main

Related

Why does Tkinter work in the terminal but not in Pycharm?

Using Pycharm on Linux mint.
I installed the "future" package for the python interpreter which I'm using. Heres the script.
from tkinter import *
top = Tk()
top.mainloop()
Didn't work. It returns "ModuleNotFoundError: No module named 'tkinter'". Tkinter is infact installed. "python3 -m tkinter" confirms it. And when I compile the same code in the terminal, it displays.
As Bryan says, you're probably not using the Python version you think you're using. PyCharm tends to install its own version of Python. Once you have more than one version of Python installed, things get trickier.
To see what's happening, try running this script:
import sys
print(sys.executable, sys.version)
Or run those similar commands from the command line. That should help clarify matters.
The sys.executable will show you the full path to your Python executable. Great for seeing where the used Python installation is located.
I don't use Python on Linux, but perhaps one of your Python installations is version 2, in which case you would need to use:
from Tkinter import *
which is another way to confirm that the Python is version 2 rather than 3. If this is the case, you'll want to move to Python 3. I don't think anyone writes new projects in Python 2 anymore. It's defunct, purely legacy.
It's also possible that Python is installed on Linux without Tkinter. There are other posts on how to install Tkinter on Linux. For instance, you can check out ImportError: No module named 'Tkinter'
Thanks guys for the help I really appreciate it. But I found out the problem was because of Linux Mint's Software Manager. I initially downloaded pycharm using said software manager but it didnt work which is why I created the post. Then I deleted it, and downloaded pycharm through the tar.gz file from the jetbrains website. After doing that, it seems to work.

How to use Ned Batchelder's script for McCabe's Complexity programmatically?

I'm trying to use Ned Batchelder's script for McCabe Complexity to get the complexity of codes written in Python 3, but I want to run it from a script instead of running it via command line.
I've already installed the McCabe from pypi and the version is 0.6.1. I'm using Python 3.8 in VSCode via Jupyter Notebook.
After reading mccabe.py from the GitHub page, I'm trying this so far:
from mccabe import *
code = open("testFile.py").read()
res = get_code_complexity(code, threshold=7, filename="testFile.py")
print(res)
However, the result I'm getting from the code above is this:
testFile.py:5:1: C901 'If 5' is too complex (63)
1
I don't know how to interpret this, because when I run the application from the command line, I get this:
$ python -m mccabe testFile.py
If 5 63
What does this mean? I thought that the complexity would be this 63, but in the res variable I'm getting 1 returned. testFile.py is in the same directory I'm running the .ipynb. I uploaded it as I think it may help.
P.S.: I know I could use Radon for this, but it seems that it only calculates the McCabe Complexity if the code is within a function, not "loose". The vast majority of the programs I'm trying to analyze don't use main(). The testFile.py I'm using as an example results in a blank list when using Radon, that's why I'm trying with this McCabe from Ned Batchelder.

How can I list all non-standard modules used by a Python program?

As some pundits say that the Python standard library is listed in the Python core documentation for your version, it is built-in by default, you don't have to install it separately from Python itself. For example, math is a standard module, you needn't install it with pip install math.
Non-standard modules are not built-in, you have to install them before you use them in a Python program. For example, lxml is a non-standard module. If not installed, a "no module named lxml" error pops up when you import lxml in the Python shell.
It is time to turn to the topic now, how can I list all non-standard modules used by a Python program?
test.py is a Python program, it is executed with python test.py. How many non-standard modules are called when python test.py is run?
pip freeze shows all installed modules, but some of them are not called by python test.py.
I was wondering the same thing, because I wanted to set up a conda environment for my project by a method other than trial-and-error. What I found worked was to make a bare environment with just python and pylint in it, using, e.g.,
conda create -n myenv python=3 pylint
and then from within that environment run:
pylint /path/to/module --disable=all --enable=import-error
This will nicely list, by file, all of the non-standard imports.

Using TensorFlow through Jupyter (Python 3)

Apologies in advance, I think the issue is quite perplexing!
I would like to use TensorFlow through Jupyter, with a Python3 kernel.
However the command import tensorflow as tf returns the error: ImportError: No module named tensorflow when either Python2 or Python3 is specified as the Jupyter kernel.
I have Python 2 and Python 3 installed on my Mac and can access both
versions through Terminal.
I installed TensorFlow for Python 3, however I can only access it via Python 2 on the Terminal.
As such, this question is really two-fold:
I want to get TensorFlow working with Python3
...which should lead to TensorFlow working with Jupyter on the Python3 terminal.
I had the same problem and solved it using the tutorial Using a virtualenv in an IPython notebook. I'll walk you through the steps I took.
I am using Anaconda, and I installed a new environment tensorflow using these instructions at tensorflow.org. After that, here is how I got tensorflow to work in a Jupyter notebook:
Open Terminal
Run source activate tensorflow. You should now see (tensorflow) at the beginning of the prompt.
Now that we are in the tensorflow environment, we want to install ipython and jupyter in this environment: Run
conda install ipython
and
conda install jupyter
Now follow the instructions in the tutorial linked above. I'll repeat them here with a bit more information added. First run
ipython kernelspec install-self --user
The result for me was Installed kernelspec python3 in /Users/charliebrummitt/Library/Jupyter/kernels/python3
Run the following:
mkdir -p ~/.ipython/kernels
Then run the following with <kernel_name> replaced by a name of your choice (I chose tfkernel) and replace the first path (i.e., ~/.local/share/jupyter/kernels/pythonX) by the path generated in step 4:
mv ~/.local/share/jupyter/kernels/pythonX ~/.ipython/kernels/<kernel_name>
Now you'll see a new kernel if you open a Jupyter notebook and select Kernel -> Change kernel from the menu. But the new kernel will have the same name as your previous kernel (for me it was called Python 3). To give your new kernel a unique name, run in Terminal
cd ~/.ipython/kernels/tfkernel/
and then run vim kernel.json to edit the file kernel.json so that you replace the value of "display_name" from the default (Python 3) to a new name (I chose to call it "tfkernel"). Save and exit vim by typing :wq while in command mode.
Open a new Jupyter notebook and type import tensorflow as tf. If you didn't get ImportError then you are ready to go!

How can I make my script choose the right python interperter?

I have both 2.7 and 3.0 versions of the Python interpreter installed (on my Ubuntu 32 system), but one particular script uses 3.0.
Using
#!/usr/bin/python3 -B
will not work when the program is run with python myprogram.py.
And I also need a solution that works also in Windows where I also have both python versions installed.
How can I make the script to run only with the right python version?
Please use virtualenv, which makes isolated Python environments easy.
python = Python to use. # This has to be the absolute path to Python executable
os.execl(python, python, * sys.argv)
This way you can restart the script with the python you want to use. Not really stylish.
I don't know why you can't just launch the program with python3 foo.py, but it's possible to have a python2 program relaunch itself as python3 with something like this.
import sys
if sys.version_info.major != 3:
import os
# replace this process with a python3 process
os.execlp("python3", "python3", *sys.argv)
It's a bad solution though, because now your python3 program can't use anything that's not valid python2 syntax
Please take a look at The wrong python interpreter is called
You have to choose a correct interpreter based on where you installed the desired version of Python and your system variables.

Resources